php trying to adding time to a variable in sql - php

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 ) );

Related

Change UTC0 to UTC +1/+2

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

Round Timestamp to a specific hour

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

Is strtotime() having a bug?

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).

Relative Date calculation off

I am trying to calculate dates relative to a certain date, but Im getting some very unusual responses. Can someone explain what I am doing wrong? I am US EST if it matters.
<?php
$firstweek_firsttime = date('D M j', strtotime("June 2016 first Sunday"));//June 19th 2016
$firstweek_lasttime = date('D M j', strtotime("June 2016 second Saturday"));
$ret=array(
"Session #1. The week of ".$firstweek_firsttime." to ".$firstweek_lasttime." - ",
"Session #2. The week of ".date('D M j', strtotime("$firstweek_firsttime next Sunday"))." to ".date('D M j', strtotime("$firstweek_lasttime next Saturday"))." - ",
"Session #3. The week of ".date('D M j', strtotime("$firstweek_firsttime +10 day"))." to ".date('D M j', strtotime("$firstweek_lasttime +10 day"))." - "
);
?>
<ul>
<?php
foreach($ret as $wk)
{
?>
<li><?php echo($wk);?></li>
<?php
}
?>
What I am getting:
The week of Sun Jun 19 to Sat Jun 18 -
The week of Thu Jan 1 to Thu Jan 1 -
The week of Wed Jul 1 to Tue Jun 30 -
Goal:
The week of Sun Jun 19 to Sat Jun 25 -
The week of Sun Jun 26 to Sat Jul 2 -
The week of Sun Jul 3 to Sat Jul 9 -
This works for me.
Where "you" are has no bearing on your dates unless you set your timezone.
The date/time is set based on the server location.
It's a bit cumbersome, if I can find a better method I'll update my answer.
UPDATE
strtotime("$firstweek_firsttime"); is the equivalent of writing strtotime("Sun Jun 5");will output 1433635200 (which, as of today, is actually Sun Jun 7 2015 00:00:00) because no year is indicated, the server defaults to the current year
strtotime("next sunday"); will output 1441497600 (which, as of today, is equal to Sun Sep 6 2015 00:00:00
but
strtotime("$firstweek_firsttime next sunday"); is invalid markup and will output nothing
so, since the timestamp is empty the date is automatically set to Jan 1, 1970
The same goes for strtotime("$firstweek_lasttime next Saturday")
strtotime("$firstweek_firsttime +10 days") is the same as strtotime("Sun Jun 5 +10 days")
without a Year the server defaults to the current year and writes it as strtotime("Sun Jun 7 2015 +10 days") because June 7 is the first Sunday for June in 2015
The same goes for strtotime("$firstweek_lasttime +10 day")
All that being said... the simple solution to your question is adding the year to your date format for $firstweek_firsttime and $firstweek_lasttime. This will keep your date in the year that you expect like so...
<?php
$firstweek_firsttime = date('D M j Y', strtotime("June 2016 first Sunday")); // Sun Jun 5 2016
$firstweek_lasttime = date('D M j Y', strtotime("June 2016 second Saturday"));
If you don't want to output the year to the browser simply change your first array item to...
"Session #1. The week of ".date('D M j', strtotime("$firstweek_firsttime"))." to ".date('D M j', strtotime("$firstweek_lasttime"))." - ",
Reference
modify date

Adding time to Nov. 2nd gives error

I'm having some problems adding a date.
$test = strtotime('nov 02 2014');
$test_date = date('D, M. jS, Y' ,(1.0*86400) + $test);
echo $test_date;
returns Sun, Nov. 2nd, 2014
changing input to nov 01 and nov 03 return the expected strings.
This should work for you:
$test = "nov 02 2014";
echo $test_date = date('D, M. jS, Y' ,strtotime($test . ' + 1 day'));
Daylight Saving Time ended on November 2, 2014. This means that November 2, 2014 lasted longer than the 86,400 seconds you're adding to the date!

Categories