I got a timestamp from my SQL-database: $DBdata = '2019-10-10 12:25:59', this date is UTC+0. Well i live in Denmark where we have the following UTC.
Central EU time winter (UTC+1)
Central EU summertime (UTC+2)
These UTC's changes from summer to winter different dates each year.
Summer time
• 2019 - The night between Saturday 30 March and Sunday 31 March
• 2020 - The night between Saturday 28 March and Sunday 29 March
• 2021 - The night between Saturday, March 27 and Sunday, March 28
• 2022 - The night between Saturday 26 March and Sunday 27 March
• 2023 - The night between Saturday, March 25 and Sunday, March 26
Winter time
• 2019 - The night between Saturday, October 26 and Sunday, October 27
• 2020 - The night between Saturday, October 24 and Sunday, October 25
• 2021 - The night between Saturday, October 30 and Sunday, October 31
• 2022 - The night between Saturday 29 October and Sunday 30 October
• 2023 - The night between Saturday 28 October and Sunday 29 October
Would it be possible to make an if-statement that changes these UTC's, with the right +1/+2?
Maybe something like this:
$Winter= gmdate('Y-m-d H:i:s', strtotime("WinterUTC"));
$Summer= gmdate('Y-m-d H:i:s', strtotime("SummerUTC"));
if (($$DBdata>= $Winter) && ($$DBdata<= $Summer)){
$gmt_dateWinter = gmdate('Y-m-d H:i:s', strtotime($date + '+ 2 hours') );
}else{
$gmt_dateSummer = gmdate('Y-m-d H:i:s', strtotime($date + '+ 1 hours') );
}
I don't know if this even is a smart way to do this. If there is another more smart way to do it please lead me in another direction.
Thanks!
With the DateTime class you can easy convert a date/time to any other time zone.
function convertTimeZone($strDateTime, $sourceTimeZone, $targetTimeZone){
return date_create($strDateTime, new DateTimeZone($sourceTimeZone))
->setTimeZone(new DateTimeZone($targetTimeZone))
->format('Y-m-d H:i:s');
}
example with a daylight saving time
$localTime = convertTimeZone('2019-10-10 12:25:59', 'UTC', 'Europe/Copenhagen');
echo $localTime."<br>";
returns:
2019-10-10 14:25:59
example wintertime
$localTime = convertTimeZone('2019-02-10 12:25:59', 'UTC', 'Europe/Copenhagen');
echo $localTime."<br>";
returns:
2019-02-10 13:25:59
Note: Solutions based on stringtotime are not recommended because of known issues and limitations on some systems.
You can use gmdate and date("I") to generate the date considering the timezone:
gmdate: Format a GMT/UTC date/time
$DBdata = '2019-10-10 12:25:59';
$timezone = +2; // (GMT +2:00) CEST (European Summer Time)
$GMdate = gmdate("Y-m-d H:i:s", strtotime($DBdata) + 3600*($timezone + date("I")));
echo $GMdate;
returns:
2019-10-10 22:25:59
From the date documentation:
I (capital i) Whether or not the date is in daylight saving time: 1
if Daylight Saving Time, 0 otherwise.
Thats simple change in php script default timezone and then display time:
https://www.php.net/manual/en/function.date-default-timezone-set.php
$time = date('H:i:s', time());
$ok = date_default_timezone_set('America/Los_Angeles');
echo $tz = date_default_timezone_get();
$h = new DateTime();
echo $h->format('H:i:s'); // curr time
$o = new DateTime($time);
echo $o->format('H:i:s'); // time
Related
I would like to get a date with strtotime.
A last Friday of a month and Saturday and Sunday if the month has sat and Sunday after last Friday.
How should I get it?
e.g. Aug 2021.
LastFriDay = 27 28 =sat 29 = sun.
I want to fetch “29. Aug”
e.g. July 2021.
LastFriday = 30 31 = sat. 1.Sep = sun
Then I want to fetch 31.Aug.
e.g Sep 2022
LastFriday = 30. 1.Oct = sat
Then The date should be 30.sep
$friOrSatOrSun = date("Y-m-d", strtotime("last fri of this month", strtotime($a_certain_day_of_month)));
Is there a good way to do that?
I have issue with Carbon liblary on PHP when modify the day (end day light saving) at UK timezone,
I'm using Carbon version: 1.31.0, PHP version: 5.6.11 on Laravel 5.5
this is my function convert to startOfHour:
public function getStartHour($timestamp)
{
$carbonDate = Carbon::createFromTimestamp($timestamp, 'Europe/London');
$startHour = $carbonDate->startOfHour()->timestamp;
return $startHour;
}
// Case 1:
// Sunday October 27, 2019 00:00:00 (am) GMT
// Sunday October 27, 2019 01:00:00 (am) (Timezone + 1 , On daylight saving)
$time = $this->getStartHour(1572134400); // 1572138000 (Issue here)
//My expected: 1572134400
// Case 2:
// Sunday October 27, 2019 00:30:00 (am) GMT
// Sunday October 27, 2019 01:30:00 (am) (Timezone + 1 , On daylight saving)
$time = $this->getStartHour(1572136200); // 1572138000 (Issue here)
//My expected: 1572134400
// Case 3:
// Sunday October 27, 2019 01:00:00 (am) UCT
// Sunday October 27, 2019 01:00:00 (am) UK (Timezone + 0)
$time = $this->getStartHour(1572138000); // 1572138000 - 01:00:00 (Ok)
// Case 4:
// Sunday October 27, 2019 01:30:00 (am) UTC
// Sunday October 27, 2019 01:00:00 (am) (Timezone + 0)
$time = $this->getStartHour(1572139800); // 1572138000 - 10:00:00 (Ok)
4 case these return same result (1572138000). It not right on the real case.
But when I use MomentJs library it converts right my expected
Here is my Js code with Moment
function getStartHour (timestamp) {
const m = moment(timestamp * 1000).tz('Europe/London');
startHour = m.clone().startOf('hour').unix()
return startHour;
}
What is different on 2 library and how can I fix PHP code return same JS code.
Thank you very much.
I start learn PHP and is very clear I make some error here, I try obtain Unix timestamp of specific hour and minutes of the day:
<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime();
$datafmt = $data->format('Y-m-d');
echo strtotime($datafmt,'18:30:00');
?>
The code return 1554951600 and is equal to:
GMT: Thursday, April 11, 2019 3:00:00 AM
Your time zone: Thursday, April 11, 2019 12:00:00 AM GMT-03:00
This is wrong, timestamp should be:
1555018200 is equal to:
GMT: Thursday, April 11, 2019 9:30:00 PM
Your time zone: Thursday, April 11, 2019 6:30:00 PM GMT-03:00
What I doing wrong?
Fixed!
echo strtotime($datafmt. '18:30:00');
, instead . that is my error!
You do not need strtotime() at all. DateTime class is a replacement and is more powerful. Just pass the time to the constructor or set it with the method setTime()
<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime('18:30:00');
// Alternative ways to set the time of the DateTime object
// $data->setTime('18', '30', '00');
// $data->setTime(...explode(':', '18:30:00'));
$datafmt = $data->format('U'); // U means UNIX timestamp
echo $datafmt;
1517317337107
it should be: Wed Jan 31 00:02:17 GMT+11:00 2018
I verify this on https://www.epochconverter.com/
the result is:
Assuming that this timestamp is in milliseconds:
GMT: Tuesday, January 30, 2018 1:02:17.107 PM
Your time zone: Wednesday, January 31, 2018 12:02:17.107 AM GMT+11:00 DST
My code in php is:
$timestamp = 1517317337107;
echo date('Y-m-d H:i:s', $timestamp);
the output is "2038-01-19 04:14:07"
what should I do?
That timestamp is in milliseconds, not seconds. Just divide it by 1000.
php > $timestamp = 1517317337107;
php > echo date('Y-m-d H:i:s', $timestamp/1000);
2018-01-30 05:02:17
Convert Your timestamp in seconds and set timezone
$timestamp = 1517317337107;
$timestamp_in_seconds = $timestamp/1000;
date_default_timezone_set("Australia/Sydney");
echo date('D M d Y H:i:s', $timestamp_in_seconds);
Output: Wed Jan 31 2018 00:02:17
I have a timestamp say 1512070200 which translates to November 30, 2017 7:30:00 PM GMT or December 1, 2017 01:00:00 AM GMT+05:30 in IST.
I want to be able to subtract the time that has passed on that day and revert back to the time at 12:00:00 AM.
For example
If i get a timestamp of 1512978955 which is December 11, 2017 7:55:55 AM GMT I want the output to be 1512950400 which is December 11, 2017 12:00:00 AM GMT.
There is no fixed amount of hours that can be subtracted from the timestamp, instead it would be a variable amount depending on the time that has passed for that particular day so it could be 1 millisecond or 1 second or 1 minute or 1 hour since 12:00:00 AM.
One way would be to convert the timestamp to a date and then break into it's constituent parts so that you can use mktime to generate the date at midday
$ts=1512070200;
$y=date('Y',$ts);
$m=date('m',$ts);
$d=date('d',$ts);
$date=date('Y-m-d H:i:s',mktime(12,0,0,$m,$d,$y));
echo date('Y-m-d H:i:s',$ts).' -> '.$date;
which outputs
2017-11-30 19:30:00 -> 2017-11-30 12:00:00
or, if the output date needs to be as shown use
$format=DATE_COOKIE;
$ts=1512070200;
$y=date('Y',$ts);
$m=date('m',$ts);
$d=date('d',$ts);
$date=date($format,mktime(12,0,0,$m,$d,$y));
echo date($format,$ts).' -> '.$date;
#Thursday, 30-Nov-17 19:30:00 GMT -> Thursday, 30-Nov-17 12:00:00 GMT
Even easier is to use noon as shown by #splash58
echo date( $format, strtotime( 'noon', $ts ) );
You can set noon time to DateTime object
$ts=1512070200;
$date = new DateTime();
$date->setTimestamp($ts);
$date->modify('noon');
echo $date->format(DATE_COOKIE); // Thu, 30 Nov 2017 12:00:00 +0000
demo