nightkarnation Posted June 26, 2012 Share Posted June 26, 2012 Hello Guys! Sorry about the Subject, didn't know how to explain it in few words... So here's the deal... $last_free_date = "Mon Jun 25 20:12:25 GMT-0300 2012" $server_current_time = date('D M j H:i:s \G\M\TO Y'); //will have the same format as the $last_free_date variable //Now here is what I need to do: //$last_format_date = $last_free_date + 24hs Any ideas?? I need to add $last_free_date + 24hs to the $last_format_date variable... Thanks a lot in advance! Cheers! Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/ Share on other sites More sharing options...
jcanker Posted June 26, 2012 Share Posted June 26, 2012 There's a couple of ways to do it---100 ways to skin a cat, right? You can always use the UNIXtime and just do the math to add: 1 minute = 60 seconds 1 hour = 60min*60sec = 3600 seconds 1 day = 24*60min*60 sec = 86400 seconds 1 Week =7*24*60*60 = 604800 seconds But the caveat to that is unless you do a little work to it first, the UNIXdate is a TIMESTAMP, so it returns the current time, so the math will be from the current time, not 00:00 of the date. The other option, and one that is becoming more popular, is to use the PHP date/time object (which also has procedural style capabilites). This is newer, so if you're running on 4.x you'll have to upgrade to 5.x. Object oriented style: <?php $date = new DateTime('2000-01-01'); $date->add(new DateInterval('P10D')); echo $date->format('Y-m-d') . "\n"; ?> Procedural style: <?php $date = date_create('2000-01-01'); date_add($date, date_interval_create_from_date_string('10 days')); echo date_format($date, 'Y-m-d'); ?> Both result in the same output PHP manual page: http://www.php.net/manual/en/datetime.add.php Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/#findComment-1357129 Share on other sites More sharing options...
nightkarnation Posted June 26, 2012 Author Share Posted June 26, 2012 Hello, first of all thanks for the reply! Still not working for me... Isn't there a simple solution? Sorry I am not so good writing php... Something similiar to this (but with some working function?): $last_format_date = $last_free_date + 24hs Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/#findComment-1357151 Share on other sites More sharing options...
jcanker Posted June 26, 2012 Share Posted June 26, 2012 assuming you have your date already formated 'Y-m-d' in a variable (here it will be $date) $newDate = date_add($date, date_interval_create_from_date_string('1 day')); From there you have the new date to reformat, output, do what you please There are lots of examples on ways to use date_add at the php manual page for it: http://www.php.net/manual/en/function.date-add.php Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/#findComment-1357159 Share on other sites More sharing options...
nightkarnation Posted June 26, 2012 Author Share Posted June 26, 2012 Jcanker thanks a lot again! I get the following error: Fatal error: Call to undefined function date_add() I have php version: 5.2.2 (locally running on wamp) Any ideas? Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/#findComment-1357169 Share on other sites More sharing options...
nightkarnation Posted June 26, 2012 Author Share Posted June 26, 2012 Nevermind! I figured it out! $Countdown_Secs = '86400'; $server_time_plus= date('D M j H:i:s \G\M\TO Y',strtotime('+'. $Countdown_Secs . 'seconds')); echo $server_time_plus; Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/#findComment-1357177 Share on other sites More sharing options...
Barand Posted June 26, 2012 Share Posted June 26, 2012 I thought your original problem was to add 24hrs to $last_free_date = "Mon Jun 25 20:12:25 GMT-0300 2012" Link to comment https://forums.phpfreaks.com/topic/264819-date-functionsadding-24hs-to-an-already-generated-date-and-such-need-help/#findComment-1357194 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.