0

I have built a countdown timer on my site which counts down the days left until the listing expires so that the clients and designers have an efficient way of keeping track of a project. When the designers are not in the office on weekends is there any way that I would be able to stop the timer when the day is a weekend?

<?php
    //start date
    //end date
    //echo date('yy-m-d H:i:s', strtotime($data->created));
    $date2 = date('yy-m-d H:i:s', strtotime($data->created . '+ '. $days));
    //echo '<br>'. $date2;
    $date = gmdate('U', strtotime($date2));
    //echo '<br>'. $date;           
    //echo '<br>' .time();
    $remaining = $date - time();
    //echo '<br>r=' . $remaining ; //get_option('date_format');2020-04-27 14:58:28
    // Get difference between both dates without DST
    $diff = $date - gmdate('U');
    // Days (in last day it will be zero)
    $diff_days = floor($remaining / (24 * 60 * 60));
    // Hours (in the last hour will be zero)
    $diff_hours = floor($remaining % (24 * 60 * 60) / 3600);
    if( $remaining >= 0 )
    {
        $days_remaining = floor($remaining / 86400);
        $hours_remaining = floor(($remaining % 86400) / 3600);
        $minutes_remaining = floor(($remaining % 3600) / 60);
        /*make red on last day*/
        $style = '';
        if($days_remaining < 1 or $days_remaining == 1 && $hours_remaining == 0){
            $style = 'style="color:#fb3a5e;"';
            echo 'Today';
        }
        else
        {
            echo '<span '.$style.'>'. $diff_days .' days and '. $diff_hours .' hours left</span>';
        }
    }
    else
    {
        echo 'Expired.';
    }
?>
Hille
  • 2,123
  • 22
  • 39
Joe Carter
  • 61
  • 6
  • 4
    Have you tried the check the day of week and then choose if your continue the countdown or not? (see [How to find the date of a day of the week from a date using PHP?](https://stackoverflow.com/a/12835399/7571526)) – Hille Aug 03 '20 at 11:40

2 Answers2

0

Just skip processing further if it matches this condition

<?php
if(date("l") != "Sunday" && date("l") != "Saturday")
{
//Process here
}
?>

You can wrap your logic inside this and it should skip the deduction of time.

Keral Patel
  • 329
  • 2
  • 11
0

The code below will fulfill your requirements.

Note: Please be aware that the above PHP statement has the timezone set to GMT and needs to be changed appropriately to your required timezone. Please also be aware that any clock discrepancies may be down to your servers timezone/clock settings.

HTML CODE:

<div id="delcta" style="<?php echo $css ?>">
<p>Time left for Free Next Day Delivery: <span id="countdowntimer"></span></p>
</div>

PHP SCRIPT:

<?php
date_default_timezone_set('GMT');
$currentTime = date('H:i:s');
$currentDay = date('w');
$delTimeStart = '00:00:00';
$delTimeEnd = '24:00:00';
if ($currentTime >= $delTimeStart && $currentTime < $delTimeEnd && $currentDay > 0 && 
$currentDay < 6){
    $css = 'display: block;';
} else {
    $css = 'display: none;';
}
?>