jarv Posted July 15, 2016 Share Posted July 15, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/ Share on other sites More sharing options...
Muddy_Funster Posted July 15, 2016 Share Posted July 15, 2016 add it where? in JS or in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534522 Share on other sites More sharing options...
jarv Posted July 15, 2016 Author Share Posted July 15, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534523 Share on other sites More sharing options...
Muddy_Funster Posted July 15, 2016 Share Posted July 15, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534524 Share on other sites More sharing options...
jarv Posted July 15, 2016 Author Share Posted July 15, 2016 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'); Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534525 Share on other sites More sharing options...
Jacques1 Posted July 15, 2016 Share Posted July 15, 2016 How about fixing the date in the database instead of messing with code hacks? Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534526 Share on other sites More sharing options...
jarv Posted July 15, 2016 Author Share Posted July 15, 2016 I would if I could find it but then the date will be wrong I think Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534527 Share on other sites More sharing options...
Muddy_Funster Posted July 15, 2016 Share Posted July 15, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534528 Share on other sites More sharing options...
Barand Posted July 15, 2016 Share Posted July 15, 2016 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 | +---------------------+------+-------+---------+---------+-----------+ Quote Link to comment https://forums.phpfreaks.com/topic/301483-adding-an-extra-day-to-a-date/#findComment-1534529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.