IMPORTANT:
In addition to above answers ,there is an important thing that one must follow.Always use the With() function(see below) i.e
Always use :
$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone));
Do not use:
$newTimezone = new DateTime($day, new DateTimeZone($timezone));
REASON:(different outputs, check below)
function with($day,$timezone){
$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone));
$timestamp = $newTimezone->format('U');
return $timestamp;
}
function without($day,$timezone){
$newTimezone = new DateTime($day, new DateTimeZone($timezone));
$timestamp = $newTimezone->format('U');
return $timestamp;
}
$tomorrow = date('Y-m-d h:i:s A', strtotime('-1 seconds ' ,strtotime('tomorrow midnight')));
$yesterday = date('Y-m-d h:i:s A', strtotime('+24 hours 1 seconds ' , strtotime('yesterday midnight')));
$timezone = 'UTC';
echo 'With Yesterday: '.with($yesterday,$timezone).'<br>';
$now = new DateTime('@'.with($yesterday,$timezone));
$now->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>';
echo 'Without Yesterday: '.without($yesterday,$timezone).'<br>';
$now = new DateTime('@'.without($yesterday,$timezone));
$now->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>';
echo 'With Tomorrow: '.with($tomorrow,$timezone).'<br>';
$now = new DateTime('@'.with($tomorrow,$timezone));
$now->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>';
echo 'Without Tomorrow: '.without($tomorrow,$timezone).'<br>';
$now = new DateTime('@'.without($tomorrow,$timezone));
$now->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>';
OUTPUTS:
With Yesterday: 1537642801
With Yesterday Readable: 09/23/2018 12:00:01 AM -------- 09/23/2018 10:05:55 PM
Without Yesterday: 1537660801
With Yesterday Readable: 09/23/2018 05:00:01 AM -------- 09/23/2018 10:05:55 PM
With Tomorrow: 1537729199
With Yesterday Readable: 09/23/2018 11:59:59 PM -------- 09/23/2018 10:05:55 PM
Without Tomorrow: 1537747199
With Yesterday Readable: 09/24/2018 04:59:59 AM -------- 09/23/2018 10:05:55 PM