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
Related
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
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;
Consider this code where we want to add or substract one second:
date_default_timezone_set("Europe/Amsterdam");
$time = 1477789199;
echo $time . ' - ' . date('r', $time) . "\n";
// 1477789199 - Sun, 30 Oct 2016 02:59:59 +0200
This is correct, as this timestamp is still just within DST (daylight savings time / summer time).
But now let's add one second to the timestamp integer, and exit DST:
$new = $time + 1;
echo $new . ' - ' . date('r', $new);
// 1477789200 - Sun, 30 Oct 2016 02:00:00 +0100
Hooray! PHP sees that one second later there is no DST anymore and shows a proper time string.
But what if we didn't add a second to the timestamp integer, but we used strtotime() to add that one second:
$new = strtotime('+1 second', $time);
echo $new . ' - ' . date('r', $new);
// 1477792800 - Sun, 30 Oct 2016 03:00:00 +0100
Yikes! We just went ahead by more than one hour instead of one second. And it doesn't even matter if you add one second, one hour, one day or one year you will always get one extra hour with it. Even if you add multiple years, you will only get one extra hour, which is weird because we enter and exit DST every year but you only get one extra hour regardless of how many years you add
But once we exit DST in October and subtract one second, all goes fine...
But then again. If we were in March and we have just entered DST, and we subtract one second, we observe exactly the same in reverse.
Wait, what ?! So ... ?
echo strtotime('+ 1 second - 1 second', 1477789199); // echoes 1477792799
Whoa ...
To me this sounds like a bug. Or is this 'by design'? Does anyone even know if this is documented somewhere, or whether it needs to be reported?
The behavior is "well documented" .... in a test:
See https://bugs.php.net/bug.php?id=30532 (which also presents your expected result as expected) and the related test file (which asserts that the current behavior is correct) https://github.com/php/php-src/blob/master/ext/date/tests/bug30532.phpt
<?php date_default_timezone_set("America/New_York");
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 EDT +1 hour'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 EDT +2 hours'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 EDT +3 hours'))."\n";
/* 2004-10-31 01:00:00 EDT
2004-10-31 01:00:00 EST
2004-10-31 02:00:00 EST */
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 +1 hour'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 +2 hours'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 +3 hours'))."\n";
/* 2004-10-31 01:00:00 EDT
2004-10-31 02:00:00 EST
2004-10-31 03:00:00 EST */
Note that in the former case the timezone (here: EDT) is being passed directly to the string, in the latter case it isn't.
In general strtotime is taking the timestamp (i.e. of 2004-10-31 - or in your specific case: the passed timestamp), converted to a representation with individual parameters, ignoring DST (i.e. individual hours, minutes, seconds, day, month, year etc.), the operation is applied to it and then converted back to the timestamp.
In particular:
echo date('r', strtotime('+ 0 second', 1477789199));
#> Sun, 30 Oct 2016 02:59:59 +0100
strtotime() throws the timezone after conversion away, i.e. only takes
Sun, 30 Oct 2016 02:59:59
and then applies the primary applicable timezone to your timezone location (i.e. Europe/Amsterdam), ending up with CET (primary!) - CEST is also possible, but only second choice.
Now, look back at the test above, just specify the originating timezone explicitly.
Thus, if you wish it to behave the way you need it:
echo date('r', strtotime('CEST', 1477789199));
#> Sun, 30 Oct 2016 02:59:59 +0200
echo date('r', strtotime('CEST + 1 second', 1477789199));
#> Sun, 30 Oct 2016 02:00:00 +0100
In fact, prepending 'CEST ' will be fine for all the times (as it will always fallback to CET if CEST is not matching and there's no overlap on CET -> CEST transition).
Time:
<?php echo date("h:i A", (strtotime($row_AllEvents['event_time'])));
I have this time in php for certain events and each event also has a specific limit.
What I want to do is add the limit to the event time and output it
the limit is stored within a sql database
like time =8 limit = 3 //output 8 - 11
This is what i have tried:
Time:
<?php echo date("h:i A", (strtotime($row_AllEvents['event_time'])+$duration));
Prints out:
Description:web
Date:September 10th, 2009
Time:12:00 AM
Location:canto
Duration:24
Description:dave
Date:June 6th, 2013
Time:02:00 AM
Location:php 123
Duration:7
Description:kajhdgfajhsdgfkjahsgdfkjahsgdfkajhsgdfkajhsgdfkajhsgdkfjhagsdkjfhagsdjf
Date:September 12th, 2013
Time:08:00 AM
Location:funny
Duration:45
Description:jack
Date:April 15th, 2014
Time:01:00 AM
Location:canto
Duration:8
Description:mickey
Date:April 15th, 2014
Time:04:00 AM
Location:canto
Duration:9
Description:high
Date:April 15th, 2014
Time:03:00 PM
Location:canto
Duration:9
Description:george
Date:April 17th, 2014
Time:03:00 AM
Location:canto
Duration:9
Description:minnie
Date:April 17th, 2014
Time:08:00 AM
Location:canto
Duration:8
Description:donald
Date:May 23rd, 2020
Time:04:12 AM
Location:canto
Duration:12
You're almost there. You need to actually add the time in seconds to your value because that's the time the date function understands.
I assume your $duration is a time in minutes, so you convert that to seconds by multiplying by 60. Or if it is hours, by 60 again.
echo date("h:i A", strtotime($row_AllEvents['event_time']));
echo ' to ';
echo date("h:i A", strtotime($row_AllEvents['event_time']) + ( $duration * 60 ) );
Can anyone help , i want to to make simple script as anyone insert human date like 2013 - 9 - 25 19:10:00 P.M to milliseconds Unix .
is it possible using strtotime.
I want anyone to show me how to make the whole human date (2013 - 9 - 25 19:10:00 P.M ) be in one variable so i can convert it then echo it
You can use strtotime or timestamp
timestamp example
$dt = new DateTime("Fri Dec 18 2013 14:25:00 GMT-0600 (MST)");
$ts = $dt->getTimestamp();
Strtotime example
$ts = strtotime("Fri Dec 09 2013 14:25:00 GMT-0600 (MST)");
echo date("r", $ts);
In your case:
$timestamp = strtotime('2013-9-25 19:10:00 P.M');