Is it possible to divide time in words in half?
For example: If I have a variable that is '24 hours' I want it to be '12 Hours' OR '13 Hours' to '6.5 Hours'.
How can I divide it in half and keep the Hours & Seconds?
Thanks !!
Is it possible to divide time in words in half?
For example: If I have a variable that is '24 hours' I want it to be '12 Hours' OR '13 Hours' to '6.5 Hours'.
How can I divide it in half and keep the Hours & Seconds?
Thanks !!
If the value is always the same format you could use explode() on the space between the amount and the word, and then divide the first element of the array by 2 and append it to the second element.
$time = '24 hours';
$pieces = explode(' ', $time);
$newtime = ($pieces[0] / 2).' '.$pieces[1];
echo $newtime;
//Should output 12 hours
You might have to use number_format() if you're expecting decimals.
Thanks for the reply!
That definitely does work, however sometimes I have for example : 1 DAY, 1 HOUR - and this breaks the code up.
Is it possible if......
I had a variable: 1 DAY, 1 HOUR
Divide it in half and get: 12 Hours, 30 Minutes
Thanks so much!
Yes, it is possible. Bonesnap's idea is a possible starting point. You need to be clear on the syntax of the input, and also on the units of time involved.
I am using a date difference script that allows me to:
$date1 = '2012-01-01 10:00:00';
$date2 = '2012-02-02 11:00:00';
$DateDiff = dateDiff($date1, $date2);
Which outputs > 1 DAY, 1 HOUR
How can I then divide this time in half, and keep Days, Hours, Minutes, Seconds ?
Let me be waaaaaaaaaaaay more basic.
If I have:
0 years, 0 months, 1 days, 1 hours, 0 minutes, 0 seconds
How can I get it to divide in HALF and be:
0 years, 0 months, 0 days, 12 hours, 30 minutes, 0 seconds
Thanks!
So what you have is a function that takes the difference between two times and then formats it as a string. Now you want to take that formatted string, parse it back into a form that can be used in calculations, halve it, and then reformat it back into a string.
Maybe rework your [font=monospace]dateDiff[/font] function so that it only does one job (taking the difference between two times) instead of two (taking the difference between two times and formatting a duration). Then have a second function that does the second job (formats a duration).
Then you'd be able to take the difference between two times, then divide it in half, and then format it.