Fun with PHP date function – In PHP set your time zone and display date and time in MySQL datetime format.

In PHP set your time zone and display date and time in MySQL datetime format.

<?php
# 'YYYY-MM-DD HH:MM:SS is "Y-m-d H:i:s"
# set the timezone to LA
$fbo=date_default_timezone_set('America/Los_Angeles');
# show that the call to set the timezone worked
echo "Timezone set? $fbo <br>";
$script_tz = date_default_timezone_get();
# MySQL datetime format
$today = date("Y-m-d H:i:s");
echo "<br>Today's date is: $today in MySQL datetime format.<br><br>";
# another way to show date
echo "Current date time is: ";
echo '<time datetime="'.date('c').'">'.date('Y-m-d H:i:s').'</time>';
#echo '<time datetime="'.date('c').'">'.date('Y-m-d').'</time>';
# output the entire PHP date format specifier table
# use the preformatted tag ("<pre>") and a PHP here document to preserve output format
echo "<pre>";
echo <<<EOF
Table 7-4. The major date function format specifiers
Format Description Returned value
Day specifiers
d Day of month, two digits, with leading zeros 01 to 31
D Day of the week, three letters Mon to Sun
j Day of the month, no leading zeros 1 to 31
l Day of week, full names Sunday to Saturday
N Day of week, numeric, Monday to Sunday 1 to 7
S Suffix for day of month (useful with specifier j) st, nd, rd, or th
w Day of week, numeric, Sunday to Saturday 0 to 6
z Day of year 0 to 365
Week specifier
W Week number of year 01 to 52
Month specifiers
F Month name January to December
m Month number with leading zeros 01 to 12
M Month name, three letters Jan to Dec
n Month number, no leading zeros 1 to 12
t Number of days in given month 28 to 31
Year specifiers
L Leap year 1 = Yes, 0 = No
y Year, 2 digits 00 to 99
Y Year, 4 digits 0000 to 9999
Time specifiers
a Before or after midday, lowercase am or pm
A Before or after midday, uppercase AM or PM
g Hour of day, 12-hour format, no leading zeros 1 to 12
G Hour of day, 24-hour format, no leading zeros 00 to 23
h Hour of day, 12-hour format, with leading zeros 01 to 12
H Hour of day, 24-hour format, with leading zeros 00 to 23
Date and Time Functions | 145
EOF;
echo "</pre>";
echo "<br>";
?>

And the output is:

Timezone set? 1
Today's date is: 2018-01-20 00:06:51 in MySQL datetime format.
Current date time is: 
Table 7-4. The major date function format specifiers
Format Description Returned value
Day specifiers
d Day of month, two digits, with leading zeros 01 to 31
D Day of the week, three letters Mon to Sun
j Day of the month, no leading zeros 1 to 31
l Day of week, full names Sunday to Saturday
N Day of week, numeric, Monday to Sunday 1 to 7
S Suffix for day of month (useful with specifier j) st, nd, rd, or th
w Day of week, numeric, Sunday to Saturday 0 to 6
z Day of year 0 to 365
Week specifier
W Week number of year 01 to 52
Month specifiers
F Month name January to December
m Month number with leading zeros 01 to 12
M Month name, three letters Jan to Dec
n Month number, no leading zeros 1 to 12
t Number of days in given month 28 to 31
Year specifiers
L Leap year 1 = Yes, 0 = No
y Year, 2 digits 00 to 99
Y Year, 4 digits 0000 to 9999
Time specifiers
a Before or after midday, lowercase am or pm
A Before or after midday, uppercase AM or PM
g Hour of day, 12-hour format, no leading zeros 1 to 12
G Hour of day, 24-hour format, no leading zeros 00 to 23
h Hour of day, 12-hour format, with leading zeros 01 to 12
H Hour of day, 24-hour format, with leading zeros 00 to 23
Date and Time Functions | 145

Reference:
http:/www.php.net/date

Leave a Comment

Scroll to Top