This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I am using this function to convert seconds to minutes:
function convert($iSeconds)
{
return date('i:s', mktime(0, 0, $iSeconds));
}
It works great. But if I use this code:
echo convert(3605);
It returns 00:05. But I need to return like this: 60:05
HOW CAN I FORMAT MINUTES:SECONDS???
You can't use mktime() or strtotime() for this kind of conevrsions. You can calculate this manually. You can try this -
function convert($iSeconds)
{
$min = intval($iSeconds / 60);
return $min . ':' . str_pad(($iSeconds % 60), 2, '0', STR_PAD_LEFT);
}
echo convert(7205);
Output
120:05
Fiddle
You can't use the date conversion function since it will not accept minutes > 60 and hours > 24.
The easiest way to do it is to use this function:
function convert($iSeconds)
{
$numMinutes = $iSeconds/60;
$numSeconds = $iSeconds - ($numMinutes * 60);
$numMinutes = $numMinutes."";
$numSeconds = $numSeconds."";
if (strlen($numMinutes) == 1)
$numMinutes = "0".$numMinutes;
if (strlen($numSeconds) == 1)
$numSeconds = "0".$numSeconds;
return $numMinutes.":".$numSeconds;
}
According to the documentation of mktime, for minute:
The number of the minute relative to the start of the hour. Negative values reference the minute in the previous hour. Values
greater than 59 reference the appropriate minute in the following
hour(s).
And for second:
The number of seconds relative to the start of the minute. Negative values reference the second in the previous minute. Values
greater than 59 reference the appropriate second in the following
minute(s).
When you call mktime you are passing a value of 0 for both hours and minutes; with 3605 seconds you are basically "spinning" the seconds hand on the clock 3600/60 times, and then you add those 5 seconds.
So you will have to first manually calculate the correct values for minutes and possibly hours that your given amount of seconds amounts to.
For that, you will need to use mod and intval of the division.
In a very educational format you'd have something like:
function convert($iSeconds) {
$seconds = $iSeconds % 60;
$minutes = intval($iSeconds/60);
$hours = intval($minutes/60);
$minutes = $minutes % 60;
return date('H:i:s', mktime($hours, $minutes, $seconds));
}
https://eval.in/508531
Indeed, this doesn't directly solve the OP, as the number of minutes can be of arbitrary size. As noted, mktime cannot be used to achieve that, as it will always format a "proper" time value, where minutes and seconds are a number between 0-59.
Related
I'm displaying videos and I want to show the duration. Im using the following:
echo ltrim(date('i:s', $vduration), '0')
//result
5:40
But, if the video is 40 seconds only. The formula doesn't work. It shows
:40
Basically, if the video is less then 60 seconds, it should show 1 zero only (not 2), like so:
0:40
Is there a magic formula for this or do I need to use conditions to check if less or equal to 60 seconds?
Instead of date(), use printf() to format the numbers the way you wan.
$minutes = date('i', $vduration);
$seconds = date('s', $vduration);
printf("%d:%02d", $minutes, $seconds);
%d will format a number with no leading zeroes, %02d will format a number in 2 digits with leading zeroes.
A date is poorly suited to represent a time interval. The DateInterval class is better suited. The date interval format method can also represent minutes without leading zeros.
//example to create a date interval
$timeStart = date_create_from_format('!','');
$timeEnd = date_create_from_format('!i:s','00:07');
$interval = $timeStart->diff($timeEnd);
//output
echo $interval->format('%i:%S'); //0:07
If the minutes and seconds are given, a date interval can also be created directly.
$minute= 1;
$second = 40;
$interval = new DateInterval('PT'.$minute.'M'.$second.'S');
This question already has answers here:
Subtract time in PHP
(3 answers)
Closed 3 years ago.
I am looking to get the previous increment of five minutes from the current time...
Lets say that the current time is 12:07pm UTC
I want to put into a variable 12:05pm UTC
What would be an easy way of going about this?
You can do this with the DateTime class (https://3v4l.org/7aqMH):
<?php
$date = new DateTime('12:07 UTC');
$minutes = (int) $date->format('i');
$rounded = round($minutes / 5) * 5;
$date->setTime($date->format('H'), $rounded, $date->format('s'));
or more concisely:
<?php
$date = new DateTime('12:07 UTC');
$date->setTime(
$date->format('H'),
round($date->format('i') / 5) * 5,
$date->format('s')
);
There's nothing built in that allows you to retrieve 'increments' however you can calculate it with minimal code. The use of modulo here allows you to figure out how far past the most recent 5 minute mark is. It will never be greater than 5 minutes (300 seconds) and can always be subtracted safely to take you back to the time you want.
$now = time();
echo $now . PHP_EOL;
$five_minutes = 60*5; // 300
$offset = $now % $five_minutes;
$five_block = $now - $offset;
echo date('Y-m-d H:i:s', $five_block);
First you will want to extract the minutes from their stored variable. Then mathematically this is simple by applying division and the floor function. To do this you can first divide by five using intdiv(numerator, denominator) which will remove any trailing decimal points and then multiply by five again to get your desired value at an increment of five.
Get the current time using time() :
$min = 300
$currentTime = time();
$mod = $currentTime % $min;
$res = $currentTime - $mod;
finalResult = date('Y-m-d H:i:s', $res);
I don't know if this will make sense but I would like to get the time from percentage of a value, I have the following as an input example:
mypercentage = 50;
mytime = "00:59:59"
As you can see, my time is 60 minutes (1 hour) and 50% of that is 30 minutes therefore from the 2 above inputs I would like to get the following output
(50 % of an hour):
00:30:00
Explode the string into an array
Convert the 3 elements into numbers
Find the value in seconds. $array[0]*3600+$array[1]*60+$array[2]
Calculate the time from the value in step 3 and the percentage.
Use /3600 and /60 to find the hour and minutes and %60 to find the seconds.
put the values in an array and implode to a string, or just use
$hour.":".$minute.":".$second
Thank you all who responded, I found this solution to work:
$mypercentage = 50;
$mytime = '00:59:59';
$totaltime = date('H:i:s', strtotime($mytime));
$seconds = strtotime("1970-01-01 $totaltime UTC");
$per_seconds = ($mypercentage / 100) * $seconds;
$time_from_percentage = gmdate("H:i:s", $per_seconds);
echo $time_from_percentage;
I need to find how much time is between to time values (their difference) which are over 24:00:00.
For example: how can I calculate the difference between 42:00:00 and 37:30:00?
Using strtotime, strptotime, etc is useless since they cannot go over 23:59:59 ....
$a_split = explode(":", "42:00:00");
$b_split = explode(":", "37:30:00");
$a_stamp = mktime($a_split[0], $a_split[1], $a_split[2]);
$b_stamp = mktime($b_split[0], $b_split[1], $b_split[2]);
if($a_stamp > $b_stamp)
{
$diff = $a_stamp - $b_stamp;
}else{
$diff = $b_stamp - $a_stamp;
}
echo "difference in time (seconds): " . $diff;
then use date() to convert seconds to HH:MM:SS if you want.
Date/Time variables and functions are not appropriate here as you're not storing time, but instead a time span of (I assume) hours, minutes, and seconds.
Likely your best solution is going to be to split each time span into their integer components, convert to a single unit (for instance, seconds), subtract them from each other, then re-build an output time span that fits with your application.
I havent tested this, but this might do what you want:
function timediff($time1, $time2) {
list($h,$m,$s) = explode(":",$time1);
$t1 = $h * 3600 + $m * 60 + $s;
list($h2,$m2,$s2) = explode(":",$time2);
$seconds = ($h2 * 3600 + $m2 * 60 + $s2) - $t1;
return sprintf("%02d:%02d:%02d",floor($seconds/3600),floor($seconds/60)%60,$seconds % 60);
}
My friend and I are working on a fairly basic uptime script for an IRC Bot.
Here's our code:
function Uptime()
{
global $uptimeStart;
$currentTime = time();
$uptime = $currentTime - $uptimeStart;
$this->sendIRC("PRIVMSG {$this->ircChannel} :Uptime: ".date("z",$uptime)." Day(s) - ".date("H:i:s",$uptime));
}
$uptimeStart is set immediately when the script runs, as time();
for some reason when I execute this function, it starts at 364 days and 19 hours. I can't figure out why.
Your $uptime is not a timestamp as should be used in date(), but a difference in time. You have an amount of seconds there, not a timestamp (that corresponds with an actual date.
just use something like this to cacluate (quick one, put some extra brain in for things like 1 day, 2 hours etc) ;)
$minutes = $uptime / 60;
$hours = $minuts/60 ;
$days = $hours / 24
etc
If you have 5.3 or above, use the DateTime and DateInterval classes:
$uptimeStart = new DateTime(); //at the beginning of your script
function Uptime() {
global $uptimeStart;
$end = new DateTime();
$diff = $uptimeStart->diff($end);
return $diff->format("%a days %H:%i:%s");
}
You won't get anything meaninful by calling date() on that time difference. You should take that time difference and progressively divide with years, months, days, hours, all measured in seconds. That way you'll get what the time difference in those terms.
$daySeconds = 86400 ;
$monthSeconds = 86400 * 30 ;
$yearSeconds = 86400 * 365 ;
$years = $uptime / $yearSeconds ;
$yearsRemaining = $uptime % $yearSeconds ;
$months = $yearsRemaining / $monthSeconds ;
$monthsRemaining = $yearsRemaining % $monthSeconds ;
$days = $monthsRemaining / $daySeconds ;
.. etc to get hours and minutes.
date() function with second argument set to 0 will actually return you (zero-date + (your time zone)), where "zero-date" is "00:00:00 1970-01-01". Looks like your timezone is UTC-5, so you get (365 days 24 hours) - (5 hours) = (364 days 19 hours)
Also, date() function is not the best way to show the difference between two dates. See other answers - there are are already posted good ways to calculate difference between years