<?php
$timezone='Asia/Tokyo';
$date_time_now = new DateTime('now', new DateTimeZone($timezone));
$date_time_now_formatted = $date_time_now->format('Y-m-d H:i:s');
echo $date_time_now_formatted . '<br>';
$UTC = new DateTime('now', new DateTimeZone('UTC'));
$UTC_formatted = $UTC->format('Y-m-d H:i:s');
echo $UTC_formatted . '<br>';
$difference_seconds = $date_time_now->getTimestamp() - $UTC->getTimestamp();
echo $difference_seconds . ' <br>';
?>
Why do i get zero? I want to calculate time difference.
3
Answers
Both of your dates are
now
, so they’re identical (you can get at most the tiny fraction of a second that it takes the computer to run both statements). In particular, DateTime::getTimestamp() returns the Unix time, which is an absolute time (a fixed moment in time). The wholeDateTimeInterface
implementation takes time zones into account, so switching a time to a different zone doesn’t make it a different (absolute) time.To calculate the difference between two local times, you can substract the timezone offsets:
Try this. here it gives difference in seconds