$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 (10ème jour du mois).
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s m es le mois'); // 17:03:18 m est le mois
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (le format DATETIME de MySQL)
FYI: there's a list of constants with predefined formats on the DateTime object, for example instead of outputting ISO 8601 dates with:
<?php
echo date('c');
?>
or
<?php
echo date('Y-m-dTH:i:sO');
?>
You can use
<?php
echo date(DateTime::ISO8601);
?>
instead, which is much easier to read.
$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 (10ème jour du mois).
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s m es le mois'); // 17:03:18 m est le mois
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s");
# 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
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
date_default_timezone_set('UTC'); // set timezone
$timestamp = time(); // get current epoch time
$format = "Y-m-d h:i:sa"; // format for date output
$formatted_date = date($format,$timestamp)); // convert timestamp to format
echo($formatted_date);
<?php
/* This answer is about date and time
The syntax for date and time is:
*/
date(format,timestamp)
/* Here, format is required and it specifies the format of the timestamp,
while timestamp is optional and specifies a timestamp. Default value
for timestamp is current date and time */
#Some characters that are commonly used for dates are:
l (Lowercase L) - Represents day of the week
d - Tells the day of the month
m - Tells the month of the year
Y - Represents a year
echo "Today is " . date("m/d/Y") . "<br>";
echo "Today is " . date("m.d.Y") . "<br>";
echo "Today is " . date("m-d-Y") . "<br>";
echo "Today is " . date("l");
#Some characters that are commonly used for time are:
H - 24-hour format of an hour (0 to 23)
h - 12-hour format of an hour (0 to 12)
i - Minute with leading zeros (0 to 59)
s - Seconds with leading zeros (0 to 59)
a - am or pm
echo "The time is " . date("h:i:sa");
echo "The time is " . date("H:i:s");
#Set timezones
date_default_timezone_set("America/New_York");
#You can access every timezone here: https://www.php.net/manual/en/timezones.php
#The timezone by default is UTC
#mktime()
mktime(hour, minute, second, month, day, year) #Syntax
$d=mktime(01, 1, 1, 1, 01, 0001);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
#strtotime
$d=strtotime("4:33am December 6 2011");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
$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>";
strtotime(time, now) #Syntax
#Due to limitations, there is another page for every other thing of Date and Time
#Search "Date and Time PHP Continued"
?>
<?php
// set the default timezone to use.
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
<?php
// Définit le fuseau horaire par défaut à utiliser. Disponible depuis PHP 5.1
date_default_timezone_set('UTC');
// Affichage de quelque chose comme : Monday
echo date("l");
// Affichage de quelque chose comme : Monday 8th of August 2005 03:12:46 PM
echo date('l jS of F Y h:i:s A');
// Affiche : July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* utilise les constantes dans le paramètre format */
// Affichage de quelque chose comme : Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// Affichage de quelque chose comme : 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
<?php
// Prints the day, date, month, year, time, AM or PM
$dollar = date("l jS of F Y") . "<br>";
echo $dollar;
// Result Example :- Wednesday 18th of August 2021
?>
<?php
// prints something like: Wednesday the 15th
echo date('l he jS');
?>
gmdate() - Format a GMT/UTC date/time
idate() - Format a local time/date as integer
getdate() - Get date/time information
getlastmod() - Gets time of last page modification
mktime() - Get Unix timestamp for a date
strftime() - Format a local time/date according to locale settings
time() - Return current Unix timestamp
DateTimeImmutable::__construct() - Returns new DateTimeImmutable object
Predefined DateTime Constants