dadamssg Posted March 6, 2009 Share Posted March 6, 2009 i am trying to calculate the number of days between a date selected and now. it will decided where to send the user based on the number. I thought it was working until right now. if you put in march 9 2009 it will calculate 2 days, which is FALSE. if i put in anything later than two days out, it will calculate a day short. heres the script <?php session_start(); include("sdateselectfun.php"); $start = strtotime($_POST['smonth']."/".$_POST['sday']."/".$_POST['syear']); $today = strtotime("today"); if($start > $today) {$difference = $start - $today;} else { header("Location: http://mysite.com/test/projectbas.php"); } $days = floor($difference/86400); // OR floor($difference/60/60/24); if ($days < 1 ) { header("Location: http://www.mysite.com/test/projectbas.php"); exit; } else { $_SESSION['days'] = $days; header("Location: http://www.mysite.com/test/pick.php?days={$_SESSION['days']}"); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/ Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi strtotime gives a number of seconds, so the difference between them for 1 day apart would be 86400. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778210 Share on other sites More sharing options...
dadamssg Posted March 6, 2009 Author Share Posted March 6, 2009 right, in my $days variable i divide by 86400 to get the days...could it be the strtotime("today")? does that grab the date and time? cause i just need the date Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778234 Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi I knocked up a tiny test and it appears to just use the date (presume it defaults the time to 00:00). Tried the difference and for me it appears to work. Only thing I can think of is possibly something to do with time zones and what your PC / server thinks the date is All the best Keith Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778240 Share on other sites More sharing options...
dadamssg Posted March 6, 2009 Author Share Posted March 6, 2009 so wierd...it will calculate the amount of days from tomorrow, and the next day after correctly, but any day after that it will calculate the number of days one short of what the actual amount should be Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778257 Share on other sites More sharing options...
dadamssg Posted March 6, 2009 Author Share Posted March 6, 2009 k, i have a SUPER wierd update. if i calculate days between today and march 6, 2010 it works..gives me 365. but ive been trying other dates such as nov 1, 2009 and today and it gives me 239 when it should be 241....what in the world is going on? i created the small form which displays the date selector and submit button as a function...im using it to display the small form in two places on my website, the form uses $_POST. is it possible that the $_POST variable values are getting mixed up???? Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778379 Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi I just tried and between today (6th March 2009 still, I am in the UK) and 1st November and I get 240 days. For now all I can suggest is that you output the 2 dates at the same time as the difference and see what they are. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778397 Share on other sites More sharing options...
Mark Baker Posted March 6, 2009 Share Posted March 6, 2009 Check whether the time component of your timstamps is taking daylight savings into account. You might need to look at using gmmktime() and gmdate() Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778401 Share on other sites More sharing options...
dadamssg Posted March 6, 2009 Author Share Posted March 6, 2009 thanks...i actually just looked into mktime() and i think thats exactly what i need. im assuming gmmktime() is the same as mktime just using gm time? also, when using mktime() how do i take into account daylight savings? thanks mark Link to comment https://forums.phpfreaks.com/topic/148236-calculating-dates/#findComment-778409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.