PHP count time and divide in daytime/nighttime

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



PHP count time and divide in daytime/nighttime



I have been trying to figure this out for a week now. My wife has started a new taxi-company and she asked me to code a simple webpage for here where she could press a button to save a timestamp, then the press is again when she gets off work, it then creates a second timestamp



I have an MYSQL database with rows containing the start time and stop time. I have managed to use the diff function to see how much time it is between the two timestamps but now comes the tricky part.



Since it's different payments at different times of the day I need to divide the time at a shortened time.



Up to 19:00 she works "daytime" and after that, she works "nighttime" until 06:00 the other day, then there is "weekend daytime" and "weekend nighttime" as well.



So if she creates a timestamp whit the date and time: 2018-08-08 06:30 and then another timestamp at 2018-08-08 21:00, then I need a script that puts these data in ex "$daytimehours = 12" "$daytimeminutes = 30" and "$nighttimehours = 3" "$nighttimeminutes = 0"



I have managed to create a script that almost works, but it is several pages long, and it contains one if-statement for each different scenario daytime-nighttime, nighttime-daytime etc.



So do anyone has a good idea on how to solve this? or maybe just point me in the right direction. I would be happy to pay some money to get this to work.




2 Answers
2



My solution is


<?php

date_default_timezone_set('Asia/Almaty');
$endDate = '2018-08-08 21:00';
$startDate = '2018-08-08 06:30';

$nightmare = date('Y-m-d 19:00');
$startDay = date('Y-m-d 06:00');

$diffMorning = strtotime($nightmare) - strtotime($startDate);
$diffNight = strtotime($endDate) - strtotime($nightmare);

echo gmdate('H:i', $diffMorning) . "n"; // this is the difference from start day till 19:00
echo gmdate('H:i', $diffNight); // this is the difference on nightmare

$total = $diffMorning + $diffNight;
echo intval($total/3600) . " hours n";
echo $total%3600/60 . " minutes n";
echo $total%3600%60 . ' seconds';



You can check via online compiler





also you can contact me on skype: kenzhegalin.serik
– Sakezzz
Aug 8 at 5:07





Thank you. Iwill try this when I get home :)
– Andreas
Aug 8 at 7:18





Okey so I got your code working now, but when I want to get a totalt amout of time worked on ie "daytime" I use this code: $dagtidhelgtotal = $dagtidhelgtotal + $dagtidhelg; But it does not take minutes in consideration, it just adds up all the hours.
– Andreas
Aug 11 at 7:34






@Andreas I updated answer
– Sakezzz
Aug 11 at 8:38





This seems good for a single day but how do you get total between dates. Post your table structure. We might be able to do it entirely in SQL.
– Sheikh Azad
Aug 11 at 11:15



given two dates stated as:


$endDate = '2018-08-08 21:00';
$startDate = '2018-08-08 06:30';



you can use the PHP Date extension to achieve the difference like this:


$start = date_create($startDate);
$end = date_create($endDate);
$boundnight = clone($end);
$boundnight->setTime(19,00);

$total_duration = date_diff($end,$start);//total duration from start to end
$day_duration = date_diff($boundnight,$start);//daytime duration
$night_duration = date_diff($end,$boundnight);// nighttime duration



you can use the format method to print a human readable string this way:


$total_duration=$total_duration->format('%H:%I');
$day_duration=$day_duration->format('%H:%I');
$night_duration=$night_duration->format('%H:%I');



At this step there is nothing left but you say you want to convert each duration in minutes.So let's build a function :


function toMinute($duration)
return (count($x=explode(':',$duration))==2?($x[0]*60+$x[1]):false);



Then you can use it this way:


$total_duration = toMinute($total_duration);
$day_duration = toMinute($day_duration);
$night_duration = toMinute($night_duration);



The output of
var_dump($total_duration,$day_duration,$night_duration) at this step is:


var_dump($total_duration,$day_duration,$night_duration)


int(870)
int(750)
int(120)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard