Jump to content

Recommended Posts

Whichever... PHP?

 

I just tried this but it came back as -16449 days 3 months -8 hours

var deadline = '". date('Y-m-d', strtotime($time. ' + 1 days')) ."';

 

at the moment the countdown says:

 

0 days 13 hours 46 minutes 30 seconds

 

I would like it to say:

 

1 days 13 hours 46 minutes 30 seconds

How about fixing the date in the database instead of messing with code hacks?

 

Exactly what I was going to suggest next.  If the data stored isn't accurate then you should be sorting that, not trying to make a workaround for it.  Also, your countdown is granular to the second and your date is only to the day.  The whole field type should be changed to datetime and the insert script is what you should be looking to alter, not the display one.

 

 

I would if I could find it...

What do you mean?

 

 

...but then the date will be wrong I think

It already is, that's kinda the issue.

try something like this

$deadline = '2016-07-16 00:00:00';  // database datetime format

$dtObj = (new DateTime($deadline))->modify('+1 days');

echo countdown($dtObj);  //---> 1 day 11 hours 37 minutes 23 seconds

function countdown(DateTime $dt)
{
    $periods =  [
                'days' => 'day',
                'h' => 'hour',
                'i' => 'minute',
                's' => 'second',
                ];
    $now = new DateTime();
    $res = $now > $dt ? 'MINUS' : '';
    $diff = $dt->diff($now);
    
    foreach ($periods as $p=>$per) {
        $v = $diff->$p;
        $plural = $v==1 ? '' : 's';
        $res .= " $v $per$plural";
    }
    return $res;
}

Alternatively, you can use SQL

SELECT deadline
, TIMESTAMPDIFF(DAY, NOW(), deadline) as days 
, MOD(TIMESTAMPDIFF(HOUR, NOW(), deadline) , 24) as hours 
, MOD(TIMESTAMPDIFF(MINUTE, NOW(), deadline) , 60) as minutes 
, MOD(TIMESTAMPDIFF(SECOND, NOW(), deadline) , 60) as seconds 
, SEC_TO_TIME(TIMESTAMPDIFF(SECOND, NOW(), deadline)) as hms
FROM datetest;

+---------------------+------+-------+---------+---------+-----------+
| deadline            | days | hours | minutes | seconds | hms       |
+---------------------+------+-------+---------+---------+-----------+
| 2016-08-01 00:00:00 |   16 |    11 |       2 |       2 | 395:02:02 |
| 2016-07-15 00:00:00 |    0 |   -12 |     -57 |     -58 | -12:57:58 |
| 2016-07-20 00:00:00 |    4 |    11 |       2 |       2 | 107:02:02 |
| 2016-07-20 18:00:00 |    5 |     5 |       2 |       2 | 125:02:02 |
+---------------------+------+-------+---------+---------+-----------+
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.