Convert days from gmdate() into hours? [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
How may I turn days from the gmdate(); into hours?
gmdate("d H:i:s", '115932'); // 02 08:12:12
What I want to return is 56:12:12
gmdate("H:i:s", '115932'); // 08:12:12
EDIT: To clarify, I just want to convert a numeric string into the H:i:s format.

To convert days d to hours, just multiply by by 24. Then add hours H.
Try this,
$date = '115932';
$minsec = gmdate("i:s", $date);
$hours = gmdate("d", $date)*24 + gmdate("H", $date);
$time = $hours . ':' . $minsec; // 56:12:12
See demo

Extract the hours, minutes and seconds, like this:
$time = 115932;
$mins = $time % 3600;
$secs = $mins % 60;
$hours = ($time - $mins) / 3600;
$mins = ($mins - $secs) / 60;
echo $hours . ':' . (($mins < 10 ) ? '0' : '') . $mins . ':' . (($secs < 10 ) ? '0' : '') . $secs;
// Or like this
vprintf('%02d:%02d:%02d',$hours,$mins,$secs);

Related

PHP: Output seconds in H:M value

I have the following code:
echo $diff . ' / ';
echo gmdate("H:i:s", $diff);
This produces
129600 / 12:00:00
However 129600 is 36 hours and not 12, so how could I amend the code to be total hours (36) rather than being 1 day and 12 hours as I don't need to show the day
So if it was 36 hours and 1 minute, I'd want to show: 36:01 or 36:1
Thanks
$hours = floor($diff/ 3600);
$minutes = floor(($diff/ 60) % 60);
$seconds = $diff% 60;
$diff= $hours . ":" . $minutes . ":" . $seconds ;
echo $diff ;
Try this. This may give you the desired result.
Fetch the days, multiply it by 24 - then add the hours to that, and append the minutes and seconds.
Note that if you have more than 30/31 days, this will stop working again and you will need to account for months or possibly years as well.
$days = gmdate("d", $diff);
$hours = gmdate("H", $diff);
echo ($days*24 + $hours).gmdate(":i:s", $diff);
You can instead go the other way around - extract the seconds and minutes!
$seconds = str_pad($diff % 60, 2, '0', STR_PAD_LEFT);
$minutes = str_pad($diff/60 % 60, 2, '0', STR_PAD_LEFT);
$hours = str_pad($diff/3600, 2, '0', STR_PAD_LEFT);
echo "$hours:$minutes:$seconds";
Live demo at https://3v4l.org/Aa8E6
As of your comment : This doesn't work if it's 08-05-2019 12:00 AM and 09-05-2019 12:00 PM - it brings back 60.
Try following code
$sstartdate = new DateTime('08-05-2019 12:00:00 AM');
$edatetime = new DateTime('09-05-2019 12:00:00 PM');
$since_start = $sstartdate->diff($edatetime);
$hours = ($since_start->days * 24)+($since_start->h);
$min = $since_start->i;
$sec = $since_start->s;
echo $hours.':'.$min;
Output: 36:00
36 hours and 00 minute

Convert Days to Year,Month,Day Format

I have this problem. Can someone help me,how to convert number of days into the format XX Years, XX Month, XX Days...
i created this function,
function convert($sum) {
$years = ($sum / 365) ;
$years = floor($years);
$month = ($sum % 365) / 30.5;
$month = floor($month);
$days = ($sum % 365) % 30.5; // the rest of days
// Echo all information set
echo 'DAYS RECEIVE : '.$sum.' days<br>';
echo $years.' years - '.$month.' month - '.$days.' days';
}
convert(151);
But with 151 days the result was wrong
DAYS RECEIVE : 151 days0 years - 4 month - 1 days
it must be 4 month ans 28 days not 1 day...
http://sandbox.onlinephpfunctions.com/code/f5e6b4b4f6a27024b66ffbf04e80698722a3ecab
If you use more modern PHP, the following is based around actual days in each month:
$days = 151;
$start_date = new DateTime();
$end_date = (new $start_date)->add(new DateInterval("P{$days}D") );
$dd = date_diff($start_date,$end_date);
echo $dd->y." years ".$dd->m." months ".$dd->d." days";
Note that it will vary, depending on the current date, so you might prefer to set $start_date and $end_date to work from a fixed baseline
$days = 151;
$start_date = new DateTime('1970-01-01');
$end_date = (new DateTime('1970-01-01'))->add(new DateInterval("P{$days}D") );
$dd = date_diff($start_date,$end_date);
echo $dd->y." years ".$dd->m." months ".$dd->d." days";
This is the solution for your problem:
function convert($sum) {
$years = floor($sum / 365);
$months = floor(($sum - ($years * 365))/30.5);
$days = ($sum - ($years * 365) - ($months * 30.5));
echo “Days received: ” . $sum . “ days <br />”;
echo $years . “ years, “ . $months . “months, “ . $days . “days”;
}

Comparing two dates results 0

so i'm currently making a small php application and I need to compare two dates to get the number of days between them.
I sadly can't use datediff() since the php version is 5.2.
I've search how to do and I found a lot of answers but I always have the same problem. When I make the difference between my dates, I always got 0 as a result.
function date_diff($dateFrom, $dateTo) {
echo $dateFrom->format('d-m-Y') . " : " . $dateTo->format('d-m-Y') . '<br/>';
$diff = abs($dateTo-$dateFrom);
return sprintf
(
"%d Days, %d Hours, %d Mins, %d Seconds",
intval( $diff / 86400 ),
intval( ( $diff % 86400 ) / 3600),
intval( ( $diff / 60 ) % 60 ),
intval( $diff % 60 )
);
}
I currently use this function and the parameters are here :
while ($donnees = mysqli_fetch_array($res))
$date = new DateTime($donnees['Date']);
$date = date_create($date->format("Y-m-d"));
$today = new DateTime();
echo $utilDate->date_diff($date, $today);
My $date and $today variables are not empty, so I don't understand why this code doesn't work.
Does anyone have an idea?
I think $dateTo and $dateFrom are objects and you are doing a substraction on them in $diff = abs($dateTo-$dateFrom);
Try $diff = abs($dateTo->getTimestamp()-$dateFrom->getTimestamp());
You want to use the diff operator:
function date_diff($dateFrom, $dateTo) {
echo $dateFrom->format('d-m-Y') . " : " . $dateTo->format('d-m-Y') . '<br/>';
$diff = $dateNow->diff($dateTo);
$day = $diff->format('%d');
$hour = $diff->format('%h');
$min = $diff->format('%i');
$seconds = $diff->format('%s');
return /*string involving above formats*/;
}

Relative Time in PHP [duplicate]

This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
I have two dates variable in php
$var1 and $var2
var1 contains some past time and var2 contains current time.I want to calculate the relative time such as 3 hrs ago,5 day ago etc
I have been stucked in this from quite a while.can anyone pls help
I already referred to the existing stackoverflow posts regarding this but it didn't help me.
you can calculate difference and then convert to natural language. And try to use Search field on StackOverflow next time ;)
Here's a function I wrote a while back to do exactly that:
function relative_date($timestamp) {
// calculate time difference
$timediff = time() - $timestamp;
switch($timediff) {
// less than a minute, show seconds
case $timediff <= 60:
$seconds = $timediff;
$str = $timediff . ($timediff == 1 ? " second ago" : " seconds ago");
break;
// less than a hour, show minutes
case $timediff <= 3600:
$minutes = floor($timediff / 60);
$str = $minutes . ($minutes == 1 ? " minute ago" : " minutes ago");
break;
// less than a day, show hours
case $timediff <= 86400:
$hours = floor($timediff / 3600);
$str = $hours . ($hours == 1 ? " hour ago" : " hours ago");
break;
// less than a year, show days
case $timediff <= (86400 * 365):
$days = floor($timediff / 86400);
$str = $days . ($days == 1 ? " day ago" : " days ago");
break;
// over a year, just show years
default:
$years = floor($timediff / (86400 * 365));
$str = $years . ($years == 1 ? " year ago" : " years ago");
}
return $str;
}

How to get Sum of time values which is in (hours and minutes) format? [duplicate]

This question already has answers here:
How we can add two date intervals in PHP
(4 answers)
Closed 8 years ago.
I want to add two time() and get the result. for example i get time1 as 1395897426 and time2 as 1395897487 now i want to add those two time in a variable time3 and convert it to hours and minutes
<?php
$time1 =time();
$time2 = time();
$time3 = $time1+$time2;
echo $time3;
?>
actually am using a function to find time difference.
function time_diff($start,$end)
{
$time1 = date('H:i',$start);
$time2 = date('H:i',$end);
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);
list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
echo "<b>$hours</b> hours and <b>$minutes</b> minutes";
}
Instead of fiddling around with it, let DateTime do all the work for you.
$time1 = new DateTime;
$time2 = new DateTime("2014-03-25 06:52:21");
$interval = $time2->diff($time1);
// Get number of days and multiply by 24 hours
$elapsed['h'] = $interval->format('%a') * 24;
// Then add in hours
$elapsed['h'] += $interval->format('%h');
// Minutes
$elapsed['m'] = $interval->format('%i');
echo $elapsed['h'] . ' hours and ' . $elapsed['m'] . ' minutes';
// produces 41 hours and 3 minutes

Categories