Skip to content

Instantly share code, notes, and snippets.

Created Dec 10, 2015
What would you like to do?
Coding Dojo - PHP Best Practices - Using DateTime
// Setting the timezone (UTC by default is STRONGLY recommended)
* Creating our initial DateTime object to the current time or 'now'.
* A value of 'null' is also equivalent to the current timestamp.
$date = new DateTime('now');
// Output with using standard date() string formatting.
echo $date->format('Y-m-d h:i:s e'), "<br/>";
// We can also manually set the timezone when instantiating our DateTime object.
$date = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
echo $date->format('Y-m-d h:i:s e'), "<br/>";
// You can also manually set the timezone after the fact if desired.
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d h:i:s e'), "<br/>";
* Modifying the date by adding a DateInterval().
* Format is 'P' for period, then integers followed by letters to indicate the
* period designation. 'T' indicates time elements.
* see:
$date->add(new DateInterval('P4DT10M')); // Added 4 days (4D) and 10 minutes (10M)
echo $date->format('Y-m-d h:i:s e'), "<br/>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment