简体   繁体   English

PHP日期功能复杂

[英]Php Date Function with complexity

My question: if the date is on Monday then my timer should start from 8:30 am and should calculate the time difference from 8:30 till date assigned, in this case its 2017-06-02 14:20:00 for first one. 我的问题:如果日期是星期一,那么我的计时器应该从上午8:30开始,并应该计算从8:30到指定日期的时差,在这种情况下,它的第一个时间是2017-06-02 14:20:00 。 So time difference should be 5 hours 50 min. 因此,时差应为5小时50分钟。

Second case, date created on 2017-06-02 09:50:00 and date assigned is: 2017-06-03 13:20:00. 第二种情况,日期创建于2017-06-02 09:50:00,分配的日期是:2017-06-03 13:20:00。 SO it should calculate from 9:50 till 9:00pm and again start from 8:30 till 13:20:00 (if next day lies on mon-sat. If next day is sun then timer should calculate from 11am till 1:20pm. and should give me duration in hours and minutes. 因此,它应从9:50到9:00 pm计算,然后从8:30到13:20:00重新开始(如果第二天是周一至周六。如果第二天是星期日,则计时器应从11am到1:20 pm计算)并且应该给我持续时间以小时和分钟为单位。

How would I do that? 我该怎么做? Its in Php & MySQL. 它在PHP和MySQL中。 I am not any frameworks or CMS systems. 我不是任何框架或CMS系统。 Its native php. 它的本地php。

My Data: 我的资料:

Date Created: 2017-06-02 02:50:00
Date Assigned: 2017-06-02 14:20:00

Date Created: 2017-06-02 09:50:00
Date Assigned: 2017-06-03 13:20:00

Mon - Sat   =   8:30am - 9:00pm
Sunday      =   11am - 5pm

Thank you in advance. 先感谢您。

Edited: This function checks first what day is your dateassigned, then calculates the time difference based on your criteria. 编辑:该函数首先检查您指定的日期是哪一天,然后根据您的条件计算时差。

<?php 

    echo getTimeLapsedCustom('2017-06-02 14:20:00') . "<br>";
    echo getTimeLapsedCustom('2017-06-03 13:20:00') . "<br>";

    function getTimeLapsedCustom($time){
        $timecreated = '';
        $timeassigned = $time;
        $datetime2 = DateTime::createFromFormat('Y-m-d H:i:s', $timeassigned);

        if($datetime2->format('D') === 'Sun'){
            $timecreated = $datetime2->format('Y-m-d') . ' 11:00:00';
        }else{
            $timecreated = $datetime2->format('Y-m-d') . ' 08:30:00';
        }


        $datetime1 = DateTime::createFromFormat('Y-m-d H:i:s', $timecreated);

        $interval = $datetime1->diff($datetime2);
        return $interval->format('%h hours %i min');
    }

Results: 结果:

5 hours 50 min
4 hours 50 min

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM