Unix seconds to clock/countdown format - php

I am currently trying to get a countdown clock using a unix timestamp value (remaining seconds) to H:M:S format, in order to sync my client side timers.
The value that I need to change to H:M:S has already been calculated as the time remaining in the countdown.
Let
$remaining_time
be our value of remaining seconds in the countdown.
Currently, here is what I have:
$H = floor($remaining_time / 3600);
$M = floor(($remaining_time - ($H*3600)) / 60);
$S = floor($remaining_time - ($H*3600 - ($M*60)));
I believe the hours/minutes is pretty close... but the seconds seems to be off. For example, I am getting results like this
$remaining_time = 4135;
Result: Time Remaining at price formatted: H:1 M:8 S:1015
Any information is appreciated - again, I need the remaining seconds in hours, minutes, and seconds.

Use gmdate, example:
<?php
$remaining_time = 4135;
echo "Time Remaining at price formatted: ".gmdate("\H:H \M:i \S:s",4135);
//Result: Time Remaining at price formatted: H:01 M:08 S:55
?>

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'";

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.

PHP's date usage results in 2 hours error

I'm using date('H:i:s', (time() - $start['time'])) to displa time that passed since start of the script.
whatever the result of time() - $start['time'] - be it 0, 17 or whatever the date prints out like 02:00:00, 02:00:17 etc.
What may be the reason?
time returns an absolute numeric timestamp, say the numeric value for 2012-06-04 16:35:12. Your start time is a similar numeric, absolute timestamp. Subtracting one from the other will result in a very small number, which is, again, an absolute timestamp. Likely some time around the beginning of 1970. When you format that timestamp using date('H:i:s'), you only display the time portion of a timestamp like 1970-01-01 02:00:00.
Read about what UNIX timestamps actually represent.
The time difference that you're looking for is the result of time() - $start['time'], which is in seconds, which you can't simply format using date(). More along the lines of:
$diff = time() - $start['time'];
echo 'Difference in seconds: ' . $diff;
echo 'Difference in minutes: ' . $diff / 60;
echo 'Difference in hours: ' . $diff / 60 / 60;
seems you have a 2H offset added.
// save current TZ
$current_tz = date_default_timezone_get();
// Use universal time
date_default_timezone_set('UTC');
$t = time();
echo "T:".$t."<br/>";
sleep(2);
echo "T2: ".date('H:i:s', time() - $t );
// return correct TZ
date_default_timezone_set($current_tz);
If i try this code without the date_default_timezone_set('UTC') it gives me expected value + 1H from Summer Time (GMT+1). If you only need the diference between the 2 times you can use UTC for both and you only get the diference OR you can check the current time offset and subtract it.
Hope it helps

PHP time subtraction not functioning

I'm trying to display the time x started, the time x finished, and how long x took to complete. I have the start and end displaying correctly, but the following subtraction gives me a bonkers answer.
// to unix timestamps for subtraction
$startTime = strtotime($row['bp_rec_start']);
$endTime = strtotime($row['bp_rec_end']);
$timeTaken = $endTime - $startTime;
//back to date formats
$startTime = date('H:i',$startTime);
$endTime = date('H:i',$endTime);
$timeTaken = date('H:i',$timeTaken);
e.g. ( 01:24 - 01:23 = 07:01)
Thanks
Timestamps are seconds since 1970, each timestamp representing an absolute point in time. So $endTime - $startTime produces some point in time like 1975-04-12 07:01:52. Printing the hour and minute part of that will of course print 07:01. The timestamp itself though is the difference in seconds, so you can do:
echo "Difference: $timeTaken seconds";
You should of course look into DateInterval (look at the 3rd example).

Categories