PHP Free Tutorial

Date and Time in PHP

In PHP, Programming by Baqir Ali

Date() Function in PHP

The date() function is used to format a local time, date, or both. Example

date("d-M-Y") date("M/d/Y")
date("D dS M, Y h:i a")
date("\T\h\e \\t\i\m\e \i\s: h:m a") date("\T\o\d\a\y \i\s: l")

Date and Time Formatting Options

OptionDescription
aam or pm
AAM or PM
BSwatch Internet Time
dDay of the month, two digits with leading zeros (01 to 31)
DDay of the week in text, three letters (Mon for Monday)
FMonth in text (January)
gHours, 12-hour format without leading zeros (1 to 12)
GHour, 24-hour format without leading zeros (0 to 23)
hHour, 12-hour format with leading zeros (01 to 12)
HHour, the 24-hour format (00 to 23)
iMinute with leading zeros (00 to 59)
I1 if daylight savings time, 0 if not
jDay of the month without leading zeros (1 to 31)
lDay of the week in text (Friday)
L1 if it is a leap year, 0 if not
mMonth with leading zeros (04 to 11)
MMonth in text in three letters (Jul)
nMonth without leading zeros ( 1 to 12)
rRFC 822 formatted date (i.e Thu, 06 Feb 2021 16:01:07 +2000)
sSeconds with leading zeros (00 to 59)
SEnglish ordinal suffix, textual, two characters(th, nd)
tNumber of days in given Month (28-31)
TTimezone settings
USeconds since the epoch
wDay of the week numeric (0 sunday to 6 saturday)
YYear four digits (2012)
yyear two digits (21)
zDay of the Year (0 to 365)
ZTimezone offset in seconds

Example 1: Date and Time in Php using different formating options. You can remove comment and test one line at a time

<?php
// $date = date("d-m-y");
// $date = date("d-m-Y");
// $date = date("d-M-Y");
// $date = date("D-M-Y");
// $date = date("dS");
// $date = date("F");
// $date = date("l");
$date = date("l, dS F, Y s");

echo($date);

?>

Output: Tuesday, 09th February, 2021 45

GetDate() function in PHP

The getdate() function returns an associative array containing date and time values for the local time.

Key Value
Seconds Seconds, range 0 to 59
minutes Minutes, range 0 to 59
hours Hours, range 0 to 23
mday Day of the month, range 1 to 31
wday Day of the the week, range 0 (Sunday), through 6(Saturday)
mon A month, range 1 through 12
year A year in four–‐digit format, such as 1999 or 2006
yday Day of the year, range 0 through 365
weekday The  day  of  the  week,  range  Sunday through Saturday
month The month, such as January or December

Example 2: Printing the complete structure of getDate() function

<?php
$date = getdate();

echo("<pre>");
print_r($date);
echo("</pre>");

?>

Output:

Array
(
    [seconds] => 4
    [minutes] => 10
    [hours] => 8
    [mday] => 9
    [wday] => 2
    [mon] => 2
    [year] => 2021
    [yday] => 39
    [weekday] => Tuesday
    [month] => February
    [0] => 1612854604
)

Example 3: Using the getDate function Array in Code

<?php
extract(getdate());

echo("Seconds = $seconds<br/>"); 
echo("Minutes = $minutes<br/>"); 
echo("Hours = $hours<br/>");
echo("Day of The Month = $mday<br/>"); 
echo("day Of The Week = $wday<br/>"); 
echo("Month = $mon<br/>");
echo("Year = $year<br/>");
echo("day of the year = $yday<br/>"); 
echo("day of the week = $weekday<br/>"); 
echo("Month = $month<br/>");

?>

Output:

Seconds = 20
Minutes = 17
Hours = 8
Day of The Month = 9
day Of The Week = 2
Month = 2
Year = 2021
day of the year = 39
day of the week = Tuesday
Month = February

Validating Dates in PHP

The checkdate() function checks whether the arguments it receives are valid date values. It returns TRUE if the date is valid, and FALSE if not. Valid date values are:

  • The month must be between 1 and 12 inclusive.
  • The day must be within the allowed number of days for the given month, and leap years are allowed.
  • The year must be between 1 and 32767 inclusive.

Example 4: Checking if a date provided is valid or not

<?php
echo "\nIs 12/31/2006 a valid date? "; 
echo "<br>";
var_dump(checkdate(12, 31, 2006)); 
echo "<br>";

echo "\nIs 12/32/2006 a valid date? "; 
echo "<br>";
var_dump(checkdate(12,32, 2006));
echo "<br>";

echo "Is this better? ";
// Let mktime() adjust the date to the correct date echo date('m/d/Y', mktime(0,0,0, 12,32,2006)),"\n"; echo "\nIs 2/29/2008 a leap year? "; var_dump(checkdate(2, 29, 2008));
echo "<br>";
echo "\nIs 2/29/2006 a leap year? "; 
echo "<br>";
var_dump(checkdate(2, 29, 2006));

?>

Output:

Is 12/31/2006 a valid date?
bool(true)
Is 12/32/2006 a valid date?
bool(false)
Is this better?
Is 2/29/2006 a leap year?
bool(false)

Time Function in PHP

  • The time() function returns the current timestamp in seconds.
  • For a more accurate time, you can use the microtime() function.
  • By adding or subtracting the number of seconds from the output of the time() function, you find the amount of time from now until some future or past time.
Unit of Time Seconds
Hour 60 * 60
Day 60 * 60 * 24
Week 60 * 60 * 24 * 7
Month 60 * 60 * 24 * 30
Year 60 * 60 * 24 * 365
date("M-d-Y", time()),"\n"; // Today
date("M-d-Y", time() + (7 * 24 * 60 * 60)); // Next week
date("M-d-Y", time() - (7 * 24 * 60 * 60)); // Last week

Mktime() Function in PHP

  • This function returns the timestamp for a particular date.
  • This timestamp is an integer containing the number of seconds between the UNIX epoch (January 1 1970 00:00:00 GMT) and the time specified in the argument list.
Argument Meaning
Hour Number of Hours
Minute Number of minutes
Second Number of seconds
Month Number of the month
Day Number of the day
Year Number of the year, can be a two or four year digit value

Example 5: Using mktime() function in PHP

<?php
$seconds=mktime(0, 0, 0, 7, 19, 2007); print "$seconds\n"; // 1184828400
echo date("M-d-Y", $seconds), "\n"; // Jul-19-2007 date("M-d-Y", mktime(0, 0, 0, 7, 18, 2007));
echo "<br>";
//Jul-18-2007
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 07));
echo "<br>";
//Jan-01-2007
echo date("M-d-Y", mktime(0, 0, 0, 7, 32, 7));
//Aug-01-2007
echo "<br>";

?>

Output:

1184796000 Jul-19-2007
Jan-01-2007
Aug-01-2007

Example 6: Using mktime() function in PHP

<?php
$today=mktime(0,0,0, date("m"),date("d"),date("Y")); echo strftime("Today is %m/%d/%Y",$today),".<br>";
$tomorrow = mktime(0, 0, 0, date("m"),date("d")+1, date("Y"));

echo strftime("Tomorrow is %A", $tomorrow),".<br>";
$yesterday = mktime(0, 0, 0, date("m") , date("d")-1, date("Y"));

echo strftime("Yesterday was %A", $yesterday),".<br>";
$nextmonth = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));

echo strftime("Next month is %B", $nextmonth),".<br>";
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));

echo strftime("Last month was %B",$lastmonth),".<br>";
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);

echo strftime("Next year is %Y", $nextyear),".<br>";
$lastyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")-1);

echo strftime("Last year was %Y", $lastyear),".<br>";

?>

Output:

Today is 02/09/2021.
Tomorrow is Wednesday.
Yesterday was Monday.
Next month is March.
Last month was January.
Next year is 2022.
Last year was 2020.

Support us by sharing this post