This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
The PHP date() function is used to format a date and/or a time.
The PHP date() function formats a timestamp to a more readable date and time.
date(format,timestamp)
Parameter | Description |
---|---|
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
<?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"); ?>
Use the
function to automatically update the copyright year on your website:© 2010-<?php echo date("Y");?>
<?php echo "The time is " . date("h:i:sa"); ?>
Note that the PHP date() function will return the current date/time of the server!
<?php date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa"); ?>
mktime(hour, minute, second, month, day, year)
The example below creates a date and time with the
function from a number of parameters in the function:<?php $d=mktime(11, 14, 54, 8, 12, 2014); echo "Created date is " . date("Y-m-d h:i:s a", $d); ?<
The PHP
function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).strtotime(time, now)
The example below creates a date and time from the
function:<?php $d=strtotime("10:30pm April 15 2014"); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?>
PHP is quite clever about converting a string to a date, so you can put in various values:
<?php $d=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $d) . "<br>"; ?>
However,
is not perfect, so remember to check the strings you put in there.The example below outputs the dates for the next six Saturdays:
<?php $startdate = strtotime("Saturday"); $enddate = strtotime("+6 weeks", $startdate); while ($startdate < $enddate) { echo date("M d", $startdate) . "<br>"; $startdate = strtotime("+1 week", $startdate); } ?>
The example below outputs the number of days until 4th of July:
<?php $d1=strtotime("July 04"); $d2=ceil(($d1-time())/60/60/24); echo "There are " . $d2 ." days until 4th of July."; ?>
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
Animated PHP icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Module 6 quiz