Format cursom days/hours into 'work hours and days' - php

I'm making a quoting system and I need a way to turn
1 days, 15 hours into 2 days, 7 hours where each day contains 8 hours.
I have access to both the days and hours variables so a function could be
function formatWorkHours($days, $hours){
}
Can anyone help me with this please? Other examples could be 1 days, 26 hours.
Thanks!

Something like this should do the trick:
function formatWorkHours($days, $hours){
$days += floor( $hours / 8 );
$hours = $hours % 8;
echo "$days days, $hours hours";
}
formatWorkHours( 1, 15 ); //echoes "2 days, 7 hours"
formatWorkHours( 1, 26 ); //echoes "4 days, 2 hours"

Related

PHP compare date_diff to lapse of time

If I have
$time_interval = date_diff(date1, date2)
How can I do this in PHP?
If ($time_interval >= ('2 months and 15 days'))
echo "time is '2 months and 15 days' or more"
else
echo "time is less than '2 months and 15 days'"
I tried
if ($time_interval->m <= 2 and $time_interval->d < 15)
But this will return FALSE for 1 month and 20 days which is obviously wrong
Is there something like..?
$time_lapse = create_my_own_time_lapse(2months & 15 days)
Then it would be very neat to compare both
If ($time_interval >= $time_lapse)
SOLUTION
date_diff retuns a DateInterval object. I found the way to create my own DateInterval for '2 months and 15 days'. This is my updated code:
Visit PHP DateInterval manual for details
$today = new DateTime(date('Y-m-d'));
$another_day = new DateTime("2019-05-10");
$time_diff = date_diff($today, $another_day);
// 'P2M15D' is the interval_spec for '2 months and 15 days'
$time_interval = new DateInterval('P2M15D');
// Let's see our objects
print_r($time_diff);
print_r($timeInterval);
if($time_diff >= $time_interval)
echo "<br/>time is '2 months and 15 days' or more";
else
echo "<br/>time is less than '2 months and 15 days'";
your code is almost correct. Just remove the and and add strtotime()
from:
if ($time_interval >= ('2 months and 15 days'))
echo "time is '2 months and 15 days' or more";
else
echo "time is less than '2 months and 15 days'";
to:
if ($time_interval->getTimestamp()) >= strtotime('2 months 15 days'))
echo "time is '2 months and 15 days' or more";
else
echo "time is less than '2 months and 15 days'";
Easy way of doing this is converting your time to seconds and than compare these seconds with amount of seconds equal to 2 months and 15 days.
$timeInterval = strtotime('2009-12-01') - strtotime('2009-10-01');
$overSeconds = 60 * 60 * 24 * 75; // 60 seconds * 60 minutes * 24 hours * 75 days
if($timeInterval >= $overSeconds)
echo "time is '2 months and 15 days' or more";
else
echo "time is less than '2 months and 15 days'";

PHP strtotime always use 31 days months

I need to calculate a duration that is expressed in human readable format (e.g. "3 months 10 days") and make it a timestamp.
My problem is that with my code uses strtotime, so if a month is 30 days long PHP interprets it as 0 months 30 days instead of 31 days.
What I need is to convert, for example, the string "1 month 10 days" to "31 days 10 days" (or "41 days").
$strToTime = strtotime($pMsg);
$unixTimestamp = $strToTime - time();
$unixTimestampM = $unixTimestamp;
$months = floor((float)$unixTimestampM/2678400);
if($months >= 1) {
$unixTimestampM -= $months*2678400;
}
$days = floor((float)$unixTimestampM/86400);
if($days >= 1) {
$unixTimestampM -= $days*86400;
}
and so on for hours and minutes.
With the string "1 month 10 days" I expect the $months to be 1 and $days to be 10, while, in certain periods of the year, $days become 9.

Carbon convert no of days to human readable format

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).

Converting minutes to hours issue in php

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.

Given number of hours get number of months, days, hours, and minutes PHP

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

Categories