1
2<?php
3// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
4// Mountain Standard Time (MST) Time Zone
5
6$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
7$today = date("m.d.y"); // 03.10.01
8$today = date("j, n, Y"); // 10, 3, 2001
9$today = date("Ymd"); // 20010310
10$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
11$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
12$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
13$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
14$today = date("H:i:s"); // 17:16:18
15$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
16?>
17
18
1
2Things to be aware of when using week numbers with years.
3
4<?php
5echo date("YW", strtotime("2011-01-07")); // gives 201101
6echo date("YW", strtotime("2011-12-31")); // gives 201152
7echo date("YW", strtotime("2011-01-01")); // gives 201152 too
8?>
9
10BUT
11
12<?php
13echo date("oW", strtotime("2011-01-07")); // gives 201101
14echo date("oW", strtotime("2011-12-31")); // gives 201152
15echo date("oW", strtotime("2011-01-01")); // gives 201052 (Year is different than previous example)
16?>
17
18Reason:
19Y is year from the date
20o is ISO-8601 year number
21W is ISO-8601 week number of year
22
23Conclusion:
24if using 'W' for the week number use 'o' for the year.
25
1
2FYI: there's a list of constants with predefined formats on the DateTime object, for example instead of outputting ISO 8601 dates with:
3
4<?php
5echo date('c');
6?>
7
8or
9
10<?php
11echo date('Y-m-d\TH:i:sO');
12?>
13
14You can use
15
16<?php
17echo date(DateTime::ISO8601);
18?>
19
20instead, which is much easier to read.
21
1
2<?php
3// set the default timezone to use. Available since PHP 5.1
4date_default_timezone_set('UTC');
5
6
7// Prints something like: Monday
8echo date("l");
9
10// Prints something like: Monday 8th of August 2005 03:12:46 PM
11echo date('l jS \of F Y h:i:s A');
12
13// Prints: July 1, 2000 is on a Saturday
14echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
15
16/* use the constants in the format parameter */
17// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
18echo date(DATE_RFC2822);
19
20// prints something like: 2000-07-01T00:00:00+00:00
21echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
22?>
23
24
1
2<?php
3// prints something like: Wednesday the 15th
4echo date('l \t\h\e jS');
5?>
6gmdate() - Format a GMT/UTC date/time
7idate() - Format a local time/date as integer
8getdate() - Get date/time information
9getlastmod() - Gets time of last page modification
10mktime() - Get Unix timestamp for a date
11strftime() - Format a local time/date according to locale settings
12time() - Return current Unix timestamp
13DateTimeImmutable::__construct() - Returns new DateTimeImmutable object
14Predefined DateTime Constants
15
1<?php
2
3// Prints the day, date, month, year, time, AM or PM
4$dollar = date("l jS \of F Y") . "<br>";
5
6echo $dollar;
7
8// Result Example :- Wednesday 18th of August 2021
9
10?>
11