It is always important to retrieve date() and time() in php. It is information which needs to be used in every system. For example, when an employee enters the company, or when the solar eclipse will happen, etc. The formatting and timezone of the date and time function are the key factors in determination of timestamp. Below, you can learn the basics of date and time functions. To practice this, you can sign up on the Cloudways Managed PHP Hosting and launch a PHP Server Stack on any of the available cloud infrastructure options.
Date():
It is used for formatting of date and time. It makes the timestamp more readable.
Syntax:
Date (format, timestamp)
You can see here, we have two parameters ‘format’ and time stamp. Let’s discuss both one by one.
Format: It is a mandatory field. Here you have to specify the format of the timestamp. For example: y/m/d or y-m-d.
Timestamp: This is an optional field. By default it selects the current date and time.
Definition of Timestamp:
Timestamp is basically a character sequence which denotes the timing (date + time) of certain event.
How to get a date in PHP:
To get simple date, you have one of the parameters format in date(). Few characters need to be specified within the parameter.
1.’d’ for the day of month (1-31)
2.’m’ for month number (1-12)
3.’y’ stands for year(it should be 4 digit)
4.’l’ represents the day of week.
Furthermore, additional formatting can be applied through characters ‘/’,’.’ And ‘-’
Examples:
<?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?>
Output:
Today is 2016/02/02 Today is 2016.02.02 Today is 2016-02-02 Today is Tuesday
How to get a simple time?
To retrieve time you should consider the following parameters:
Some commonly used characters used in format parameter are as follows:
‘h’ shows 12 hour format (01 – 12)
‘I’ shows minutes with zeroes(00-59)
‘s’ denotes seconds (00-59)
‘a’ denotes am or pm.
Example:
<?php echo "The time is " . date("h:i:sa"); ?>
Output:
The time is 10:55:17am
Get Your Time Zone
If the time you got back from the code is not the right time, it’s probably because your server is in another country or set up for a different timezone.
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
How to check the time zone?.
You can even get a specific timezone with respect to location.
The example below sets the timezone to “America/New_York”, then outputs the current time in the specified format.
Code:
<?php date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa"); ?>
Output:
The time is 05:02:46am
Mktime():
It is used to get the Unix timestamp for a date.
Syntax:
Mktime(hour, minute, second, month, day, year)
Example:
<?php $date=mktime(11, 14, 54, 8, 12, 2016); echo "Created date is " . date("Y-m-d h:i:sa", $date); ?>
Output:
Created date is 2016-08-12 11:14:54am
Strtotime():
It is used for converting string to a unix time.
Syntax:
Strtotime(time,now)
Example:
<?php $d=strtotime("03:22pm February 2 2016"); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?>
Output:
Created date is 2016-02-02 03:22:00pm
Here are further more examples:
<?php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("next Sunday"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date) . "<br>"; ?>
Output:
2016-02-03 12:00:00am 2016-02-07 12:00:00am 2016-05-02 11:30:02am
Dates as Objects:
PHP facilitates us by providing an option to set dates as an object. Hence, via OOP we can convert a date object as follows:
<?php echo 'America/Los_Angeles:<br>'; $date = new DateTime('2016-02-02', new DateTimeZone('America/Los_Angeles')); echo $date->format('Y-m-d H:i:sP') . "<br><br>"; ?>
Output:
America/Los_Angeles: 2016-02-02 00:00:00-08:00
Hence we have created a date and time object in Pacific time zone (America/los_angeles) . we can convert this time zone into eastern time zone as follows:
echo 'America/New_York:<br>'; $date->setTimezone(new DateTimeZone('America/New_York')); echo $date->format('Y-m-d H:i:sP') . "<br><br>";
So, we have just completed the tutorial of getting date and time in PHP. I hope you find it useful. If you’ve got any questions, comments or suggestions, fire away in the comments section below!
Ahmed Khan
Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works.