Using the PHP date() function, we can format a timestamp to a much more simple and easier-to-read date and time.
date() would go like this:
Here in the date function, you have noticed two arguments.
First is the format, which is required and specifies the format of our timestamp.
Another is the timestamp itself. In case we do not provide this second parameter This function would return the current date and time.
How to Get a Date
Using the required parameter format we can specify how we show the date.
Here we some of the characters used in dates, to represent time periods:
- d – The Small d represent day of month. It will return day specified. it could be between 1 to 31.
- m – This one represent a month. It starts from 1 and ends on 12.
- Y – The big Y is used to represent year in our format, it would display year in four digits.
- l – The lowercase L is for represent day of the week, like sunday, monday, tuesday and so on.
And to separate day, month and year we would use characters like “/”, “.”, or “-“
Here are a few different examples of formatting dates:
<?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"); ?>
Print Current copyritght year using Date function
You may have noticed the copyright symbol along with the current year in the footer of various websites.
Using the PHP date function we can easily print that.
Here is how that code goes:
© 2010 - <?php echo date("Y") ?>
How to Get a Time
Similar to date, time also has a different character to represent hours, minutes, and seconds.
Some of them are:
- H – The capital H present hour but in 24-hour format which start form 0 and ends on 23.
- h – The small h represent hour in in 12-hour format, it starts from 01 and ends on 12.
- i – The small i is there of minutes wiht leading zeros.
- s – The small s here represent secods.
- a – The small a for Ante meridiem and Post meridiem in short am and pm.
Here is how you can print time using the date function:
echo "The time is " . date("h:i:sa");
How to Get your Time zone
Each country has its own time zone and if you use the date function to display the time it would show the current time according to timezone set on your computer or server.
But PHP also offers us the option to set different time zone:
Here is how you can set timezone according to specific location:
date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa");
This code displays the current time of New York city of America.
You can find the list of all supported timezones on the official PHP.Net Website.
Creating Date with mktime function
As you may know that using
date() the function we could display current date and time.
By not adding the timestamp parameter, the date function will return the current date or time.
We can also use
mktime() the function to return the Unix timestamp of the date and then assign it to a variable.
Here is the syntax of mktime function:
mktime(hour, minute, second, month, day, year)
We can put that variable to date function something like this:
$d=mktime(11, 14, 54, 8, 12, 2022); echo "Created date is " . date("Y-m-d h:i:sa", $d);
Creating Date from string with strtime function
Using PHP function
strtotime() we can convert a date into a Unix timestamp.
and after that we can use that as a timestamp in our date function:
$d=strtotime("10:30pm April 15 2014"); echo "Created date is " . date("Y-m-d h:i:sa", $d);
If you want to know more about PHP Date and time, you can access complete references from W3Schools.Com.