0

So, have a plugin that's a bit busted, it's meant to do a count down till next days opening hour. It seems to be adding on an additional 1.5 hours, i've tried a few things and nothing seem to to have an effect on it.

Original Code:

    // JS counters data
$right_now     = current_time( 'Y-m-j' );
$time_now      = current_time( 'timestamp' );

$tomorrow_time = date( 'Y-m-j', strtotime('+1 day', $time_now ) );

if ( $opening_time < $closing_time ) {
    $to_time   = $right_now . ' ' . $closing_time;
    $from_time = $tomorrow_time  . ' ' . $opening_time;
} else {
    $to_time   = $right_now . ' ' . $closing_time;
    $from_time = $right_now . ' ' . $opening_time;
}

I've tried this:

// JS counters data
$right_now     = current_time( 'Y-m-j' );
$time_now      = current_time( 'timestamp' );

$hours_fix = current_time( 'timestamp', strtotime('-1 hours, -30 minutes') );
$tomorrow_time = date( 'Y-m-j', strtotime('+1 day', $hours_fix ) );

if ( $opening_time < $closing_time ) {
    $to_time   = $right_now . ' ' . $closing_time;
    $from_time = $tomorrow_time  . ' ' . $opening_time;
} else {
    $to_time   = $right_now . ' ' . $closing_time;
    $from_time = $right_now . ' ' . $opening_time;
}

And a few other bits similar to adding an additional line to cover the 1.5 hours also tried changing the +1 day to +22 hours but the time doesn't move it's still 1.5 hours a head of what it should be, (i can't see any fixed timezone settings in the code so it should be pulling the time from the Wordpress install which is the correct time! Any one got any points here?

2
  • Where is this code coming running? Where do the values come from? And which value is being increased by an hour and a half? There are a lot of time variables in your code, and some are used but never defined ( e.g. it's never explained where $opening_time or $closing_time come from ), can you edit your question to clarify things? Which timezone is your server running with?
    – Tom J Nowell
    Oct 20, 2019 at 22:57
  • I also notice in your second code block, you pass the result of strtotime into current_time as a second parameter, which is confusing because the second parameter of that function is a true/false value according to the official documentation. Can you explain the reasoning behind this and what you hoped it would do?
    – Tom J Nowell
    Oct 20, 2019 at 22:58

1 Answer 1

0

The current_time() function whenever passed a timestamp as a parameter will return a UNIX Timestamp which is already been run through strtotime(). By passing that variable to strtotime() you're trying to timestamp a timestamp. Try this instead:

$tomorrow_time = date( 'Y-m-j', $time_now );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.