Add one hour from _GET - php

I'm using date('H', strtotime($_GET['h'].' +1 hour')) at the moment ($_GET['h'] contains the hour I'm getting from the URL, i.e. 20 as in 20:00) but it only prints 01 when it should print 21. I want to get this hour and add one hour to it so 20 will be 21 and so on.
What am I doing wrong here?

Something like this should work:
$time = DateTime::createFromFormat('H:i', '20:00');
$time->modify('+1 hour');
echo $time->format('H:i');
See it in action

You can add seconds to the timestamp to do this:
date('H',mktime($_GET['h']) + 60 * 60)
Since the timestamp must be in seconds, the first 60 * 60 gets the hour in seconds. The second one adds an hour's worth of seconds to the timestamp.

Related

MySQL add time to time stamp

I'm trying to run a query where a check is done if a time is in between a time and that time + 90 mins.
I have 1 time value being passed in. So lets say the time being passed in is 1pm. I'd like to check if a time is between 1pm and 2.30pm.
I have tried passing in a second param adding the seconds with strtotime() and I have the below
AND r.start_time BETWEEN '19:00:00.0000' AND '19:00:00.0000', INTERVAL + 90 MINUTE
I think you can use strtotime(...)+5400 to express 90 minutes.
For example:
$times = strtotime('19:00:00.0000');
$end_time = $times + 5400;
$query = "SELECT ... WHERE r.start_time BETWEEN '$times' AND '$end_times'";

PHP - unix timestamp from seconds to milliseconds

So I need to convert an unix timestamp which is in seconds to milliseconds.
This line seems to be not working
$unixtime = strtotime($timestamp_conv."+3 hour") * 1000;
I basically need a 13 digit timestamp.
Any ideas what I'm doing wrong?
Thanks
As per the comments, $timestamp_conv already is a (second-)timestamp, you want to convert to a (milliseconds-)timestamp. You, however, also try to add some offset (3 hours) to it.
With simple arithmetics this would look like this
// add the three hours: 3 hours of 60 minutes of 60 seconds each
$timestamp_conv += 3 * 60 * 60;
// convert to milliseconds base
$unixtime = $timestamp_conv * 1000;
You can use DateTime from php, it's more intuitive.
<?php
$t = new \DateTime();
$t->setTimestamp(1492498242);
$t->modify("+3 hours");
echo $t->getTimestamp()*1000;
Hope it helps!

Add 30 seconds interval in a given datetime

Need some help.
I am using PHP.
So I have coordinates data.
Specifically Longtitude and Latitude.
So let's say I have 15 data of Long and Lat to be inserted on a table.
However, the api gives me only a single datetime because these coordinates are in array.
For example:
[14.4364372;121.0125753, 14.4364375;121.0125755, 14.4364377;121.0125758, 14.436436;121.012574, 14.4364342;121.0125721, 14.4364326;121.0125704, 14.436433;121.0125707, 14.4364334;121.0125711, 14.4364338;121.0125716, 14.4364342;121.012572, 14.4364345;121.0125724, 14.4364348;121.0125728, 14.4364351;121.0125731, 14.4364353;121.0125733, 14.4364356;121.0125735]
So first you will explode it to get rid of the delimeter ','
And loop it to count how many are data,then explode again the delimiter ';' to count the long and lat given.
But it only have a single datetime.
What i want here is to insert these data including datetime but with interval of 30 seconds per insert.
How can i do that?
Expected output would be like this:
INSERT INTO table_gps(ticket,datetime,long,lat) VALUES(0,09/16/2016 03:30:26 pm, 14.4364363,121.0125745)
INSERT INTO table_gps(ticket,datetime,long,lat) VALUES(0,09/16/2016 03:30:56 pm, 14.4364364,121.0125746)
Here is my code:
$msg= 'YC GPS2~09/16/2016 13:29:46~-~[14.4364362;121.0125744, 14.4364363;121.0125745]';
if(strpos($msg,'YC GPS2')!==false){
//explode data
$explode=explode('~',$msg);
$ky=$explode[0];
$datetime=$explode[1];
$ticket=$explode[2];
$coords=$explode[3];
$coords=explode(',',$coords);
for($i=0;$i<count($coords);$i++){
$xpVal=$coords[$i];
if($xpVal){
$xp3=explode(';',$xpVal);
$lng=$xp3[0];
$lat=$xp3[1];
$lng=str_replace('[','',$lng);
$lat=str_replace(']','',$lat);
$time = date("m/d/Y h:i:s a", time() + 30);// where 30 is the seconds
echo "INSERT INTO GPS2(ticket,datetime,long,lat) VALUES(".$ticket.",".$time.",".$lng.",".$lat.") ";
}
}
}else{
echo '0';
}
Thanks.
In order to add how many seconds you want to particular date in PHP you can use the following.
$time = date("m/d/Y h:i:s a", time() + 30);// where 30 is the seconds
The following is really easy way to add days, minutes, hours and seconds to a time using PHP. Using the date function to set the format of the date to be returned then using strtotime to add the increase or decrease of time then after a comma use another strtotime passing in the start date and time.
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
Times can be entered in a readable way:
+1 day = adds 1 day
+1 hour = adds 1 hour
+10 minutes = adds 10 minutes
+10 seconds = adds 10 seconds

Difference between 2 dates in days using php

I have a problem with my script and I dont understand where is the problem. So I have this code :
$i_now = strtotime(date('Y-m-d'));
$i_date_last_bonus = strtotime($o_member->date_last_bonus);
$i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
print_r("Date Now :".date('Y-m-d'));
print_r("Last Win :".$o_member->date_last_bonus);
I get the $i_datediff = 1 and I dont understand why because in the print_r I have Date Now :2015-12-04 and Last Win:2015-12-03
Can you help me please where I make the error ? Thx in advance and sorry for my english
In one day there are 24 Hrs, in each hour there are 60Min, in each min there are 60sec. Hence, there are 24*60*60 = 86400sec in one day.
Now, The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Means it returns seconds.
so, i_now = 1449187200 andi_date_last_bonus = 1449100800
Difference is 86400sec.
Now $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400); this is converting seconds in days.
And Difference is 86400 / 86400 = 1
means 1 day.
This result is correct as you get the number of seconds between two dates first and then you divide it by the number of seconds in 24 hours (86400) which gives you 1 (day) as a result.

How can i find the timestamp for the next starting minute?

I want to do something like this
strtotime("next starting minute");
If the current time is 15:23:21, i want to get 15:24:00
Any ideas?
Don't use strtotime() but use time() and ceil().
$next_minute_timestamp = ceil(time()/60)*60;
This way, easy calculated you will always get the timestamp for the next minute. Because I divide the timestamp by 60, it will return the number of minutes. Ceiling this will get you the next minute. Then the timestamp again is multiplied by 60 to get the seconds, which is the timestamp you need.
$timestamp = mktime(date('H'), date('i') + 1, 0);
strtotime(date('H') . ":" . (date('i') + 1) . ":00");
Get the time, subtract the seconds, add 60 seconds for one minute:
$timestamp = time()-(time()%60)+60;
my solution , it worked for me.
$nexminute = date("H:i", time()+60).":00";

Categories