Jump to content
jarv

adding an extra day to a date

Recommended Posts

So I am declaring deadline as a JS variable and then going into PHP

 

 

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

 

I just want to add an extra day onto my deadline

 

any help here please?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I'm still not sure about the code you are using. var is used in JS and the $ prefix is used in php, so it looks like you are mashing the two together.

 

What is $time? and where does it come from?

Share this post


Link to post
Share on other sites

Yes it's all a bit mashed together

 

 

$time variable is a date set in the database

 

 

$time = $context->data->field_event_date[0]['value'];
 
var deadline = '". date('Y-m-d', $time) ."';
 
initializeClock('clockdiv', deadline);", 'inline');
 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I would if I could find it but then the date will be wrong I think

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 |
+---------------------+------+-------+---------+---------+-----------+

Share this post


Link to post
Share on other sites

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.