Last active
August 29, 2015 14:07
-
-
Save fragoulis/d2be339a04fbba676cf8 to your computer and use it in GitHub Desktop.
Get current datetime with timezone and microsecond precision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate datetime with timezone support and microsend precision. | |
* | |
* Example: | |
* ``` | |
* $now = Datetime::now(); // generates YYYY-MM-DDTHH:MM:SS.MMMMMM | |
* ``` | |
* | |
* @author John Fragkoulis <[email protected]> | |
**/ | |
class Datetime | |
{ | |
public static $defaultTimezone = 'Europe/Athens'; | |
public static $defaultFormat = 'Y-m-d\TH:i:s.u'; | |
public static function now($format=null, $timezone=null) | |
{ | |
$format = $format ? $format : static::$defaultFormat; | |
$timezone = $timezone ? $timezone : static::$defaultTimezone; | |
$datetime = date_create_from_format('U.u', microtime(true)); | |
$datetime->setTimezone(new DateTimeZone($timezone)); | |
return $datetime->format($format); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Supported by PHP 5.3 or greater