<?php
// To change the format of an existing date
$old_date_format = "20/03/1999";
$new_data_format = date("Y-m-d H:i:s", strtotime($old_date_format));
<?php
echo date("F j, Y, g:i a")."<br>"; // March 10, 2001, 5:16 pm
echo date("m.d.y")."<br>"; // 03.10.01
echo date("j, n, Y")."<br>"; // 10, 3, 2001
echo date("Ymd")."<br>"; // 20010310
echo date('h-i-s, j-m-y, it is w Day')."<br>"; // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
echo date('i is he jS day.')."<br>"; // it is the 10th day.
echo date("D M j G:i:s T Y")."<br>"; // Sat Mar 10 17:16:18 MST 2001
echo date('H:m:s m is mo
h')."<br>"; // 17:03:18 m is month
echo date("H:i:s")."<br>"; // 17:16:18
echo date("Y-m-d H:i:s")."<br>"; // 2001-03-10 17:16:18 (the MySQL DATETIME format)
date("D M j G:i:s T Y")."<br>"; // Sat Mar 22 17:16:18 MST 2001
date('H:m:s m is mo
h')."<br>"; // 18:03:18 m is month
date("H:i:s")."<br>"; // 18:16:18
date("Y-m-d H:i:s")."<br>"; // 2022-03-11 18:16:18 (MySQL DATETIME format)
date("j F, Y, g:i a")."<br>"; // 11 March, 2022, 6:16 pm
date("m.d.y")."<br>"; // 03.11.01
date("j, n, Y")."<br>"; // 11, 3, 2001
date("Ymd")."<br>"; // 20010310
date('h-i-s, j-m-y, it is w Day')."<br>"; // 02-12-18, 10-03-01, 1631 1618 6 Satpm01
date('i is he jS day.')."<br>"; // it is the 15th day.
date_default_timezone_set('asia/karachi'); // set timezone
$timestamp = $row['last_login']; // get current epoch time
$format = "Y-m-d h:i:s a"; // format for date output
$formatted_date = date($format, $timestamp); // convert timestamp to format
print($formatted_date);
# From a date Object:
date_format ( DateTimeInterface $object , string $format )
# From the current time
$dateTime = new DateTime();
// or pass a string or int ->`DateTime($date_time)`
$dateTime->format('y-j-d H:i:s T'); #print ex: 21-2-02 16:00:01 PST
# Or a quick one-liner:
date('g:i A m-d-Y', strtotime($date_time)); #print ex: 2:00 PM 02-02-2021
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
//
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('i is he jS day.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s m is mo
h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>
a - "am" or "pm"
A - "AM" or "PM"
d - day of the month, 2 digits with leading zeros; "01" to "31"
D - day of the week as text, 3 letters; "Mon"
F - month as text, full name; "January"
h - hour, 12-hour format; "01" to "12"
H - hour, 24-hour format; "00" to "23"
g - hour, 12-hour format without leading zeros; "1" to "12"
G - hour, 24-hour format without leading zeros; "0" to "23"
i - minutes; "00" to "59"
j - day of the month as a number without leading zeros; "1" to "31"
l (lowercase L) - day of the week as text, full name; "Monday"
L - boolean for if it is a leap year; "0" or "1"
m - month as a number; "01" to "12"
n - month as a number without leading zeros; "1" to "12"
M - month as text, 3 letters; "Jan"
s - seconds; "00" to "59"
S - English ordinal suffix as text, 2 characters; "th", "nd"
t - number of days in the given month; "28" to "31"
U - seconds since the epoch
w - day of the week as a number; "0" (Sunday) to "6" (Saturday)
Y - year, 4 digits; "1999"
y - year, 2 digits; "99"
z - day of the year; "0" to "365"
Z - timezone offset in seconds ("-43200" to "43200")