I have a timestamp like 3:07:01 pm. Regardless of the number of seconds, I always need to round up this timestamp to 3:08:00 pm. How can I achieve this? I have tried using the ceiling function like so:
$time_rounded_up_to_nearest_minute = ceil(round(time() / 60) * 60);
but this function gives me some weird behavior around the 30 second mark. As soon as 30s mark hits it goes to the next minute, which is not what I want. I want it to always go to the nearest minute regardless of seconds
Don't use round(), since it rounds to the nearest integer, it doesn't round up. Just use ceil() by itself.
$time_rounded_up_to_nearest_minute = ceil(time() / 60) * 60;
Related
In php i have two times - 11:00:00 and 12:45:00. I want to get the difference between them in minutes, in this case 105 minutes. Whats the best way that can be done?
Thank you!
Here you go:
( strtotime('12:45:00') - strtotime('11:00:00') ) / 60
strtotime() is a very useful function. It returns the Unix timestamp for a wide variety of times and dates. So, if you take the two timestamps, and subtract them, then you have the difference in seconds. Divide by 60 to get the minutes.
$time_diff = strtotime('2013-03-13 12:45:00') - strtotime('2013-03-13 11:00:00');
echo $time_diff/60;
I just kept dates as not sure if I keep the time part only it would return the correct diff or not.
EDIT
I just tested it works without date too ...
$time_diff = strtotime('12:45:00') - strtotime('11:00:00');
echo $time_diff/60;
So to answer you question - strtotime() returns a timestamp (the number of seconds since January 1 1970 00:00:00 UTC) so you simply divide it by 60 to convert it result into minutes.
This seems like there should be a very easy solution, however everything I'm trying is either giving me an error or the wrong result.
I'm pulling data from a MySQL table and the time is stored in the Epoch format in the database. When I make the query on the website it's showing: 3672 (the same number shown in the database). I've tried using the date() function, a number of different str* functions, different arithmetic operations, however nothing is giving me the actual time, which should be showing as: '1:02'.
I'm not trying to pull the date, actual time, etc. I'm just trying to convert an Epoch time string to a traditional 'H:mm' format, because these are for durations, not timestamps.
Any help would be greatly appreciated!
As others already noticed, this is not really any standard epoch time but simply number of seconds since midnight.
Looking at your example, you only need hours and minutes (rounded up). This will give you those:
$h = (int)($number / 3600);
$m = ceil(($number - $h * 3600) / 60);
$result = sprintf('%d:%02d', $h, $m);
Dividing number by 3600 (60 for seconds * 60 for minutes) will give you number of hours. Using your example of 3672 this will give you 1.
To get minutes you just remove hours to get seconds (72) and then divide that by 60 to get minutes (1 minutes and 12 seconds). Since your example specifies 1:02 is result, you can simply take next upper integer (2).
At end result is 1:02, as you specified.
I need to get the difference of hours from between two timestamps. Can you tell me how can it be done using php?
Thanks in advance.
Regards,
$hours = (abs(strtotime($timestamp1)-strtotime($timestamp2)) / 60) / 60;
You can feed the values of timestamps into strtotime(), this will get you the UNIX TIME STAMP in seconds since 1970. It'll be two large integer second values. So then do a subtraction between the two values and convert that into whatever you're looking to get. e.g.
minutes, hours, days, weeks, etc... by doing normal *60, *60, *24, *7 computation
Use strtotime to convert the timestamps into seconds, subtract them, use abs to get the absolute value (no negative numbers here!), divide by 3600 seconds (1 hour), and then round to the precision you'd like.
$difference = round(abs(strtotime($stamp_one) - strtotime($stamp_two)) / 3600, 2);
Bah, I've been fiddling on how to do this. I need a function that returns <hour>:<minutes> that is rounded to a quarter, but need to be atleast 30 minutes in future time.
Anyone got a good idea and how to do this?
Add 30 minutes.
Extract date + hour on the one hand side, minutes on the other.
Divide minutes by 15, ceil the result, multiply by 15.
Build new date using date + hour and add the new minutes.
$current = date("Hi");
$ceilUp = ceil($current / 15);
$timeNeeded = $ceilUp + 30;
something like that? (I see it's not exactly what you need.. but this is a start).
I am trying to calculate the age of something in hours.
$data['record'] is a mysql NOW() timestamp in a DATETIME field.
$data['record'] is 20 minutes old, when I do :
$minutes= date('i',(strtotime("now")-strtotime($data['record'])));
$minutes returns 20 properly, however for some reason when I try $hours it returns '5'.
$hours = date('g',(strtotime("now")-strtotime($data['record'])));
This does not make sense, as $hours should be returning 0 as the record is less than 60 minutes old...
When I checked the value of "strtotime("now")-strtotime($data['record'])" it is equal to '980'. Help!
Please compare the output of strtotime("now") of php and select now(); in sql. I think there is a timezone problem hidden here.
As you said, strtotime("now")-strtotime($data['record']) returns 980, which should be in minutes. 960 is divideable by 60 and comes out at 16 hours, so 980 is 16 hours 20 minutes - the 20 minutes are exactly what you are looking for. You'll need to adjust either instance to use the time of the other - I would go with always using UTC. If you need to display it, parse it appropiately and output the local time.
Please See: http://php.net/manual/en/function.date.php
When the $format parameter="g", it returns a value 1-12.
Date will not quite work like you're expecting it to.
Date takes a time stamp (# of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)), and converts that into a legible time format. Essentially, with a value of 980, you are going to get January First at midnight + 980 seconds (roughly January 1 1970 00:16:20 GMT. When you convert for the time zone difference, (chances are, about 5 hours difference) that's how you get five.
To fix this, simply take 980, and divide by 60 to get minutes, then divide by 60 again to get hours, so:
$hours = ((strtotime("now")-strtotime($data['record'])) / 60) / 60;
There's no need for date, as you need a relative time, not an absolute time.