D
D
Dorothy2017-02-02 19:45:00
Working with date/time
Dorothy, 2017-02-02 19:45:00

How to get midnight timestamp after time zone change?

Hello.
I'm using Carbon which is a DateTime extension for working with datetime.
There is a task to learn time of midnight for the set timestamp of date for the set time zones.
For example, there is a timestamp = 1485943200 and you need to find out the midnight timestamp for Moscow and Samara on that day.

$initial = Carbon::createFromTimestamp(1485943200);
$moscow_midnight = $initial->copy()->timezone('Europe/Moscow')->modify('midnight')->getTimestamp();
$samara_midnight = $initial->copy()->timezone('Europe/Samara')->modify('midnight')->getTimestamp();

The result is
$moscow_midnight = 1485907200
$samara_midnight = 1485907200
which corresponds to midnight simply by 0 GMT, and not by the specified time zones.
It is possible to get the right time if you only create a Carbon object immediately with an indication of the time zone and what you need to get.
$moscow_midnight = (new Carbon('midnight', 'Europe/Moscow'))->getTimestamp();
$samara_midnight = (new Carbon('midnight', 'Europe/Samara'))->getTimestamp();

It turns out
$moscow_midnight = 1485982800
$samara_midnight = 1485979200

Which is true for the current day. But this method is not quite what I need. Since it is necessary to calculate midnight relative to the date specified in $initial, and indeed, if the first method worked, it would be more convenient, since there I simply change the time zone of the end objects and take what I need.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Deyashkin, 2017-02-02
@Dorothy

The point is that it ->modify("midnight")does not set the time to the beginning of the day. There is no such modifier in the documentation . To set the start of the day you can use ->startOfDay().
So your code looks like this:

$initial = Carbon::createFromTimestamp(1485943200);
$moscow_midnight = $initial->copy()->timezone('Europe/Moscow')->startOfDay()->getTimestamp();
$samara_midnight = $initial->copy()->timezone('Europe/Samara')->startOfDay()->getTimestamp();

You also need to understand that in the code that you have, "this day" means a day in the UTC time zone. If you are interested in a day in a different time zone, you need to do it differently.
I would advise replacing createFromTimestampwith createFromTimestampUtc- so the behavior of the script will be more transparent/obvious. Although, in fact, it will not change, but if, say, someone debugs it, a "strange" situation may turn out. If you run the code in phpio.net/tools/carbon
echo '<br />';require 'Carbon/Carbon.php';
use Carbon\Carbon;

$initial = Carbon::createFromTimestamp(0);
echo "{$initial} {$initial->tzName}<br />";
$samara = $initial->copy()->timezone('Europe/Samara')->startOfDay();
echo "{$samara} {$samara->tzName}<br />";

$initial = Carbon::createFromTimestampUtc(0);
echo "{$initial} {$initial->tzName}<br />";
$samara = $initial->copy()->timezone('Europe/Samara')->startOfDay();
echo "{$samara} {$samara->tzName}<br />";

its output will be like this:
1969-12-31 18:00:00 US/Central
1970-01-01 00:00:00 Europe/Samara
1970-01-01 00:00:00 +00:00
1970-01-01 00:00:00 Europe/Samara
And if only the time was displayed, without the time zone, it could be confusing in the first case, since the dates do not match.

A
Andrzej Wielski, 2017-02-02
@wielski

Try like this:

$initial = Carbon::createFromTimestamp(1485943200);
$moscow_midnight = $initial->copy()->setTimezone('Europe/Moscow')->modify('midnight')->getTimestamp();
$samara_midnight = $initial->copy()->setTimezone('Europe/Samara')->modify('midnight')->getTimestamp();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question