Skip to content

Instantly share code, notes, and snippets.

@thephucit
Created February 4, 2020 10:15
Show Gist options
  • Select an option

  • Save thephucit/e2d9c7c24947d95c4428c4cb946ec89d to your computer and use it in GitHub Desktop.

Select an option

Save thephucit/e2d9c7c24947d95c4428c4cb946ec89d to your computer and use it in GitHub Desktop.
Convert datetime in db into local datetime
<?php
/**
* Convert datetime in db into local datetime
* @param string $datetime
* @param string $format
* @param string $timezone
* @return String
*/
public static function convert_datetime_to_local($datetime, $format = 'H:i d-m-Y', $timezone = 'Asia/Ho_Chi_Minh')
{
$userTimezone = new \DateTimeZone($timezone);
$gmtTimezone = new \DateTimeZone('UTC');
$myDateTime = new \DateTime($datetime, $gmtTimezone);
$offset = $userTimezone->getOffset($myDateTime);
$myInterval = \DateInterval::createFromDateString((string) $offset . 'seconds');
$myDateTime->add($myInterval);
return $myDateTime->format($format);
}
@thephucit
Copy link
Copy Markdown
Author

format in js: HH:mm DD:MM:YYYY

@thephucit
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment