Viralscripts
                                 Viral Scripts
Empowering Your Mind, One Script at a Time.

Blog Full Detail

Date formates in PHP ?

PHP provides two functions for formatting dates: date() and DateTime::format().

date()

The date() function takes a Unix timestamp as input and returns a formatted date string. The format of the output string is specified by the second parameter.

Here are some examples of how to use the date() function:

PHP

// Get the current date and time.
$now = time();

// Format the date and time as andquot;2023-10-29 15:12:55andquot;.
$formattedDate = date(andquot;Y-m-d H:i:sandquot;, $now);

// Format the date as andquot;October 29, 2023andquot;.
$formattedDate = date(andquot;F j, Yandquot;, $now);

// Format the time as andquot;15:12andquot;.
$formattedTime = date(andquot;H:iandquot;, $now);

DateTime::format()

The DateTime::format() method returns a formatted date string, given a DateTime object. The format of the output string is specified by the first parameter.

Here are some examples of how to use the DateTime::format() method:

PHP

// Create a DateTime object for the current date and time.
$now = new DateTime();

// Format the date and time as andquot;2023-10-29 15:12:55andquot;.
$formattedDate = $now-andgt;format(andquot;Y-m-d H:i:sandquot;);

// Format the date as andquot;October 29, 2023andquot;.
$formattedDate = $now-andgt;format(andquot;F j, Yandquot;);

// Format the time as andquot;15:12andquot;.
$formattedTime = $now-andgt;format(andquot;H:iandquot;);

Date formats

The following table shows some common date formats that can be used with the date() function and the DateTime::format() method:

Character Description Example
d The day of the month (01 to 31) 29
D A textual representation of a day (three letters) Sun
j The day of the month without leading zeros (1 to 31) 29
l (lowercase and#39;Land#39;) A full textual representation of a day Sunday
N The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday) 7
S The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j) st
w A numeric representation of the day (0 for Sunday, 6 for Saturday) 0
z The day of the year (from 0 through 365) 303
L Whether itand#39;s a leap year (1 if it is a leap year, 0 otherwise) 1
o The ISO 8601 week-numbering year 2023
Y A four digit representation of a year 2023
y A two digit representation of a year 23
a Lowercase am or pm am
A Uppercase AM or PM AM
B Swatch Internet time (000 to 999) 1512
g 12-hour format of an hour (1 to 12) 3
G 24-hour format of an hour (0 to 23) 3
h 12-hour format of an hour (01 to 12) 03
H 24-hour format of an hour (00 to 23) 03
i Minutes with leading zeros (00 to 59) 12
s Seconds, with leading zeros (00 to 59) 55
u Microseconds (added in PHP 5.2.2) 000000
e The timezone identifier (Examples: UTC, GMT, Atlantic/Azores) PST

drive_spreadsheetExport to Sheets

Conclusion

PHP provides a variety of options for formatting dates. You can use the date() function or the DateTime::format() method, depending on your needs.