php convert number into minutes - php

Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.

Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));

You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')

Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.

$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;

Related

Round time string in gap of 30 minutes

I have a time string in UNIX format . I need to round that string to closest interval of 30 minutes.
For eg: I have time as 9:20 AM than it should round it to 9:30 AM.
If minutes are greater than 30 like 9:45 AM it should round to 10:00 AM.
I have tried this so far:
$hour = date('H', $ltdaytmfstr);
$minute = (date('i', $ltdaytmfstr)>30)?'00':'30';
echo "$hour:$minute";
$ltdaytmfstr is time string in unix format.
Any suggestions? It would be better if I can get the value returned in UNIX format .
You should try this: This will round it to the nearest half an hour.
Use ceil function.
<?php
$rounded = date('H:i:s', ceil(strtotime('16:20:34')/1800)*1800);
echo $rounded;
?>
Output: 16:30:00
http://codepad.org/4WwNO5Rt
If you use DateTime:
$dt = new \DateTime;
$diff = $dt
->add(
//This just calculates number of seconds from the next 30 minute interval
new \DateInterval("PT".((30 - $dt->format("i"))*60-$dt->format("s"))."S")
);
echo $dt->getTimestamp();
I guess this is what you are looking for
function round_timestamp($timestamp){
$hour = date("H", strtotime($timestamp));
$minute = date("i", strtotime($timestamp));
if ($minute<15) {
return date('H:i', strtotime("$hour:00") );
} elseif($minute>=15 and $minute<45){
return date('H:i', strtotime("$hour:30") );
} elseif($minute>=45) {
$hour = $hour + 1;
return date('H:i', strtotime("$hour:00") );
}
}
echo round_timestamp("11:59");
// 00:00
echo round_timestamp("10:59");
// 11:00
Since UNIX time is in seconds, you can just transform it to 30 minute units, round, and convert back to seconds.
$timestamp = time();
$rounded = round($timestamp / (30 * 60)) * 30 * 60
You can also use floor() or ceil() to round up or down if needed.

How to get the hour minute and second?

The future time is :2012-05-26 00:00:00
supposed there are three variable: $hour $minute $second
now, i want to using the future time subtract now time. then give the left hour to $hour,give the left minute to $minute,give the left second to $second.
i am sorry i am new of php, now i get stucked how to do the math operation ? thank you
A very good resource for dates and time..
http://www.php.net/manual/en/function.time.php
-there are samples here doing something similar.
Check the date_diff function. There's the exact solution to what you're asking there.
And here's the page (DateInterval::format) documenting how you can format the output.
$now = date_create();
// use "now" and necessary DateTimeZone in the arguments
$otherDate = date_create('2020-04-13');
$interval = date_diff($now, $futureDate);
echo $interval->format('%a days');
The following are the math operations for the difference in hours,minutes and seconds
$future_datetime = '2012-05-26 00:00:00';
$future = strtotime($future_datetime); //future datetime in seconds
$now_datetime = date('Y-m-d H:i:s');
$now = date('U'); //now datetime in seconds
//The math for calculating the difference in hours, minutes and seconds
$difference = $future - $now;
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$difference_hours = floor($difference/$hour);
$remainder = $difference - ($difference_hours * $hour);
$difference_minutes = floor($remainder/$minute);
$remainder = $remainder - ($difference_minutes * $minute);
$difference_seconds = $remainder;
echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds";

PHP Compare a difference of timestamp and a real time

I would like to compare a difference between two timestamps and a time in base 60 or 10.
More precisely if timestamp1-timestamp2 is longer (or no) than x hours and y seconds.
I'm using DateTime and DateInterval classes, but there isn't such a function, and i don't find a clean solution.
Thanks
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps, it can be string date too.
$interval = $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
Output
11 hours 32 minutes 4 seconds
$timestamp1 = strtotime('2012-01-26 14:00:00');
$timestamp2 = strtotime('2012-01-25 17:00:00');
if (abs($timestamp1 - $timestamp2) < 60 * 60 * 5 /* (5 hours) */) {
...
Convert both the timestamp and the real time to the UNIX timestamp. Then simply subtract to get the number of seconds difference.
With datetime objects:
$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');
more

Create Variable in PHP Equal to Current Time Minus One Hour

In PHP, how could I create a variable called $livetime that equals the current time minus 1 hour?
Another way - without all the math and, in my opinion, reads better.
$hour_ago = strtotime('-1 hour');
If you're looking for how to display the time in a human readable format, these examples will help:
$livetime = date('H:i:s', time() - 3600); // 16:00:00
$livetime = date('g:iA ', time() - 3600); // 4:00PM
$livetime = time() - 3600; // 3600 seconds in 1 hour : 60 seconds (1 min) * 60 (minutes in hour)
See time PHP function for more information.
convert your date to strtotime and then subtract one hour from it
$now = date('Y-m-d H:i:s');
$time = strtotime($now);
$time = $time - (60*60); //one hour
$beforeOneHour = date("Y-m-d H:i:s", $time);
You could use the date_create function along with the date_sub function like I have shown here below: -
$currentTime = date_create(now());
$modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
$liveTime = $modifyTime->format('Y-m-d H:i:s');
Assuming that a timestamp is fine you can use the time function like so
<?php
$livetime = time() - 60 * 60;
Current time is equal to time() (current time given in seconds after Unix epoch).
Thus, to calculate what you need, you need to perform calculation: time() - 60*60 (current time in seconds minus 60 minutes times 60 seconds).
$time_you_need = time() - 60*60;
First convert hours into seconds (3600) then use the following:
$your_date = date('F jS, Y',time() - 3600);

Can we use add time to the PHP date() function?

I am trying to make a schedule from the current hour
to 12 hours afterwards. I am using the date() function
to retrieve the current time but how can I increment the
hours and adjust the AM/PM? Can I just add 1 to the date()
function?
Thank you for your help.
$now = time();
$then = $now + 12 * 60 * 60;
echo date(format, $then);
You can use strtotime:
date('d-m-Y H:i:s', strtotime('+1 hour')); // one hour since now
or
date('d-m-Y H:i:s', strtotime('2011-02-25 14:00:42'));
$mydate = date("dateformat", time() + 43200);
time() gets the current timestamp, then you add 43200 which is 12 hours * 60 mins * 60 secs

Categories