I have this code:
$total_days = 0;
$minutes = 1200;
echo date('H:i', mktime(0, $minutes));
This return me 20 hours, which means two 8 hours + 4 hours working day. It means that in the $total_days will have 2 days + 4 h. How can I divide the 1200 minutes to get 2 days and 4 hours ?
Calculate it manually -
$minutes = 1200;
$total_hours = $minutes / 60;
echo floor($total_hours / 8). ' days ' . ($total_hours % 8). ' hours';
Output
2 days 4 hours
$total_hours contains fractinal value then the calculation should be changed accordingly.
Related
I need to convert 30 days to 1 month .If months and days are means then like 1 years 2 month 2 days
I have tried below but it will return wrong result
echo CarbonInterval::days(30)->cascade()->forHumans();
Can any one help me how i can achieve this ?
I have tried below solution but got only 2 days difference
$convert = '30'; // days you want to convert
$years = ($convert / 365) ; // days / 365 days
$years = floor($years); // Remove all decimals
$month = ($convert % 365) / 30.5; // I choose 30.5 for Month (30,31) ;)
$month = floor($month); // Remove all decimals
$days = ($convert % 365) % 30.5; // the rest of days
// Echo all information set
echo 'DAYS RECEIVE : '.$convert.' days<br>';
echo $years.' years - '.$month.' month - '.$days.' days';
Is there any good solution using carbon
Does it have to be CarbonInterval?
What about Carbon::now()->subDays(1827)->diffForHumans()?
The reason it doesn't work as you're expecting (from https://carbon.nesbot.com/docs/#api-interval):
Default factors are:
1 minute = 60 seconds
1 hour = 60 minutes
1 day = 24 hour
1 week = 7 days
1 month = 4 weeks
1 year = 12 months
CarbonIntervals do not carry context so they cannot be more precise
(no DST, no leap year, no real month length or year length
consideration).
i need to convert number of seconds to date, and then show time difference as days, hours and seconds.
But for some reason after some number of days, in last hour before next full day I get negative hours value. Right now this problem occures if date is greater then 38 days (it's before 1st November). Maybe tomorrow this value will be different, I'm not sure.
Code:
$s = 84600; // 23.5 h in seconds
$s += (60*60*24)*38; // add 38 days in seconds
$d = (new \DateTime())->modify("-".$s."seconds");
echo (new \DateTime())->diff($d)->format("%a days %h:%I");
// shows: 38 days -1:30
Same code with 1 day of difference:
$s = 84600; // 23.5 h in seconds
$s += (60*60*24)*37; // add 37 days in seconds
$d = (new \DateTime())->modify("-".$s."seconds");
echo (new \DateTime())->diff($d)->format("%a days %h:%I");
// shows: 37 days 23:30
PHP version 5.6.2. Tested on localhost, server and http://sandbox.onlinephpfunctions.com/ with same result.
Not sure if you looking for this kind of work around
<?php
$s= 84600;
$ts= strtotime('38 day 30 second', 0);
$difference=$ts-$s;
$days = floor($difference / 86400);
$hours = floor(($difference - $days * 86400) / 3600);
$minutes = floor(($difference - $days * 86400 - $hours * 3600) / 60);
$seconds = floor($difference - $days * 86400 - $hours * 3600 - $minutes * 60);
echo "{$days} days {$hours} hours {$minutes} minutes {$seconds} seconds";
?>
//Output 37 days 0 hours 30 minutes 30 seconds
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 7 years ago.
I have two date times of the form
Start Date: 2015-11-15 11:40:44pm
End Date: 2015-11-22 10:50:88am
Now I need to find the difference between these two in the following form:
0 years, 0 months, 7 days, 22 hours, 44 mints, 35 sec
How can I do this in PHP?
I already try:
$strStart = date('Y-m-d h:i:s', time() - 3600);
$strEnd = '2015-11-22 02:45:25';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
echo $dteDiff->format("%H:%I:%S");
Output:22:53:58
Output not shown perfectly.
Now I need to find the difference between these two in the following form:
0 years, 0 months, 7 days, 22 hours, 44 mints, 35 sec
So that’s your main problem here, getting this exact output structure?
Well then you simply have to format the DateInterval differently:
echo $dteDiff->format("%y years, %m months, %d days, %h hours, %i mints, %s sec");
$startDate = "2015-11-15 11:40:44pm";
$endDate = "2015-11-22 10:50:48am"; // You had 50:88 here? That's not an existing time
$startEpoch = strtotime($startDate);
$endEpoch = strtotime($endDate);
$difference = $endEpoch - $startEpoch;
The script above converts the start and end date to epoch time (seconds since January 1 1970 00:00:00 GMT). Then it does the maths and gets the difference between them.
Since years and months aren't a static value, I haven't added them in the script below
$minute = 60; // A minute in seconds
$hour = $minute * 60; // An hour in seconds
$day = $hour * 24; // A day in seconds
$daycount = 0; // Counts the days
$hourcount = 0; // Counts the hours
$minutecount = 0; // Counts the minutes
while ($difference > $day) { // While the difference is still bigger than a day
$difference -= $day; // Takes 1 day from the difference
$daycount += 1; // Add 1 to days
}
// Now it continues with what's left
while ($difference > $hour) { // While the difference is still bigger than an hour
$difference -= $hour; // Takes 1 hour from the difference
$hourcount += 1; // Add 1 to hours
}
// Now it continues with what's left
while ($difference > $minute) { // While the difference is still bigger than a minute
$difference -= $minute; // Takes 1 minute from the difference
$minutecount += 1; // Add 1 to minutes
}
// What remains are the seconds
echo $daycount . " days ";
echo $hourcount . " hours ";
echo $minutecount . " minutes ";
echo $difference . " seconds ";
I'm calculating the difference between two dates (the current date and a date in the database). I want to display the difference in days and hours between the dates, but when the day-difference is 0 I don't want to display it. This is my code:
<?php
$to_time = new \DateTime($database_time);
$from_time = new \DateTime();
echo $from_time->diff($to_time)->format("%d %H");
?>
Output should be, for example:
> 50 days and 4 hours
> 1 day and 7 hours //and not 1 dayS
> 6 hours //and not 0 days and 6 hours
There are two issues with my own code: the first one is that it always display the number of days. The second one is that it is just a format. For example when the difference is 2 months, 6 days and 2 hours it displays: 6 days and 2 hours. But it should be 68 days and 2 hours (because of the two months).
Can't get it work with other codes I found. Thanks in advance!
Try this..
$seconds = strtotime("2012-10-10 02:40:03") - strtotime("2010-12-25 05:15:02");
echo $days = floor($seconds / 86400);
echo "</br>";
echo $hours = floor(($seconds - ($days * 86400)) / 3600);
echo "</br>";
echo $minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
echo "</br>";
echo $seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));
I know there might be different ways using timestamps and stuff but I'm having trouble converting number of hours into something that human would understand. I do not have power to change anything in the database.
There is a column that holds number of hours, so it can be something like 134.37 hours. Now I can not display that and tell user that something will happen in 134.37 hours I need to convert it into months, days, hours, minutes, seconds.
For example:
Given Hours: 23.33
Desired Result: 0 Months, 0 Days, 23 Hours, 19 Minutes, 48 seconds (dont care about seconds)
Now I need months and days since number of hours might be large. The code I started with does give me number of hours, minutes and seconds but i cant get days and months.
$months = $days = $hour = $min = $sec = 0;
$decimalHours = 23.33;
//convert to hours
$hour = (int)$decimalHours;
$decimalHours -= $hour;
//convert to minutes and subtract minutes
$decimalHours *= 60;
$min = (int)$decimalHours;
$decimalHours -= $min;
$decimalHours = number_format($decimalHours, 10);
//convert to seconds
$decimalHours *= 60;
$sec = (int)$decimalHours;
echo $hour . ' hours, ' . $min . ' minutes, ' . $sec . ' seconds';
Please help if you know a function that does it or an easier way.
You can achieve this with DateTime extension:
$hours = 23.33;
$zero = new DateTime('#0');
$offset = new DateTime('#' . $hours * 3600);
$diff = $zero->diff($offset);
echo $diff->format('%m Months, %d Days, %h Hours, %i Minutes');
demo
Code new DateTime('#0'); creates DateTime object with timestamp 0, which is January 1 1970 00:00:00 GMT. Timestamp 0 is zero number of seconds since the Unix Epoch.In this example it basically doesn't matter how you create DateTime object, I just wanted it to be in UTC offset and to ignore DST. You can also create DateTime object like new DateTime('UTC'); (which is current datetime in UTC timezone) or something familar.
Edit:
I guess I can ignore months and display days + hours + minutes is better than just hours
In that case just use echo $diff->format('%a Days, %h Hours, %i Minutes');. See the difference where I replaced format of days from %d to %a. Read the DateInterval::format() what this characters mean. You can also access parameters directly on DateInterval objects as echo $diff->days; echo $diff->h; // etc. (use print_r($diff); to see those parameters).
How long is a month? 30 days? 31 days? 30.5 days? 365.24 / 12 ?
Skipping that, you can do:
$hours = 23.33;
$days = floor($hours / 24);
$remaining_hours = $hours - $days * 24;
$hours = floor($remaining_hours);
$minutes = round(($remaining_hours - $hours) * 60);
echo $days . " days " . $hours . " hours " . $minutes . " minutes";
// 0 days 23 hours 20 minutes
First off the hours thing is bonkers. I'm assuming they are always adjusted to be current (ie 10 hours to something happening... 9 hours.. 8 hours)
But have you tried a simple php strtotime() approach? Format your output to a date/time/countdown using timestamp?
$dateFromToday = strtotime('+23.33 hours'); // get unixtimestamp from today + hours
echo date('l jS \of F Y h:i:s A', $dateFromToday); // format my date output
Maybe I am oversimplifying it tho.
If you populate the $datetime variable (sorry for unconventional variable names), the following code applies.
This:
$original = 23.33;
$hours = floor($original);
$minutes = floor(60 * ($original - $hours));
echo sprintf('Total: %s hours, %s minutes', $hours, $minutes);
echo '<br />';
$datetime = new \DateTime('-2 hours 15 minutes');
$destined = new \DateTime(sprintf('+ %s hours %s minutes', $hours, $minutes));
echo sprintf('Scheduled Time: %s', $destined->format('Y-m-d sH:i:s'));
echo '<br />';
$interval = $destined->diff($datetime);
echo sprintf('Time Remaining: %s months, %s days, %s hours, %s minutes',
$interval->m, $interval->d, $interval->h, $interval->i);
Outputs:
Total: 23 hours, 19 minutes
Scheduled Time: 2014-01-04 1915:15:19
Time Remaining: 0 months, 1 days, 1 hours, 4 minutes