You can format a date into the desired format using the DateTime
class in PHP. The following code shows how to create a DateTime
object for the current date and time, and then format it to the desired format:
main.php98 chars4 lines
The format()
method accepts a format string as a parameter, and returns a string representing the formatted date. In the format string, Y
represents the year, m
represents the month, d
represents the day, H
represents the hour, i
represents the minute, s
represents the second, u
represents the microsecond, and T
represents the timezone abbreviation.
The resulting output will be in the format yyyy-mm-dd hh:mm:ss.fff zzz
, where yyyy is the four-digit year, mm is the two-digit month, dd is the two-digit day, hh is the two-digit hour in 24-hour format, mm is the two-digit minute, ss is the two-digit second, fff is the three-digit microsecond, and zzz is the timezone abbreviation.
gistlibby LogSnag