RE: [PHP] Date +30 comparison
-Original Message- From: tedd [mailto:tedd.sperl...@gmail.com] Sent: 01 September 2009 21:52 At 2:47 PM -0400 9/1/09, Andrew Ballard wrote: On Tue, Sep 1, 2009 at 1:27 PM, teddtedd.sperl...@gmail.com wrote: First get the date to seconds, like so: $today_date = '8/26/2009'; $next_date = strtotime($today_date) + (86400 * 30); No. Due to Daylight Saving Time, many time zones have two days each year when the number of seconds in a day is not 86400. Arrggg. But good to know. And if you absolutely insist on doing it this way, make sure you start in the middle of the day -- if your base time is 12:00 noon (which is what I always use in this situation), the furthest it can go because of DST is 11:00 or 13:00, which won't screw you up if all you're interested in is the date. ;) Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Leeds Metropolitan University, C507, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Date +30 comparison
At 4:06 PM +0100 9/2/09, Ford, Mike wrote: -Original Message- From: tedd [mailto:tedd.sperl...@gmail.com] Sent: 01 September 2009 21:52 At 2:47 PM -0400 9/1/09, Andrew Ballard wrote: On Tue, Sep 1, 2009 at 1:27 PM, teddtedd.sperl...@gmail.com wrote: First get the date to seconds, like so: $today_date = '8/26/2009'; $next_date = strtotime($today_date) + (86400 * 30); No. Due to Daylight Saving Time, many time zones have two days each year when the number of seconds in a day is not 86400. Arrggg. But good to know. And if you absolutely insist on doing it this way, make sure you start in the middle of the day -- if your base time is 12:00 noon (which is what I always use in this situation), the furthest it can go because of DST is 11:00 or 13:00, which won't screw you up if all you're interested in is the date. ;) Cheers! Mike Another good thing to know. Thanks, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date +30 comparison
On Tue, 2009-09-01 at 12:19 -0400, David Stoltz wrote: I'm really struggling with dates in PHP. (Yes, I tried reading the manual)... Can someone provide code that does this: Takes current date, assigns it to a variable (let's say $today) Then adds 30 days to $today variable Takes a string ($nexteval) like '8/26/2009' and compare it to $today. The variable $nexteval must be greater than $today (which is today + 30 days) or a message is echoed. I'm finding this difficult to do, but I'm coming from an ASP background. Any help appreciated. PHP (like all languages I know) treats dates as numbers; and PHP specifically uses seconds since January 1st 1970 (other languages sometimes have different start points and can measure in milliseconds instead). With this in mind, you can compare dates directly as you would an integer, and the later date will be the higher value. To add 30 days to a given date, you could use the date_add function (http://uk2.php.net/manual/en/datetime.add.php ) which has various formats you can use to add different time units. Lastly, to turn a date like 8/26/2009, I would probably try to break it down into it's component parts (maybe using explode('/', $string_date) ) and then using those values as arguments in a mktime() function. PHP should automatically treat the values as integers if they are strings, because like ASP, it uses loose typing on variables. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date +30 comparison
I'm really struggling with dates in PHP. (Yes, I tried reading the manual)... Can someone provide code that does this: Takes current date, assigns it to a variable (let's say $today) Then adds 30 days to $today variable Takes a string ($nexteval) like '8/26/2009' and compare it to $today. The variable $nexteval must be greater than $today (which is today + 30 days) or a message is echoed. I'm finding this difficult to do, but I'm coming from an ASP background. Any help appreciated. David, Look up date() and mktime() in the manual. To get today's date is easy: date('m/d/Y'); But when wanting to add days/months/years to a date, you need to use mktime() mktime() breaks apart a date into hours/minutes/seconds/months/days/years. Therefore I always break up my date into these types fo sections. $month = date('m'); $day = date('d'); $year = date('Y'); Now that I have them seperated I create a variable $future_date and assign it the value I want. In your case, 30 days in the future. $future_date = mktime(0,0,0,date($month),date($day)+30,date($year)); Now I put it back into a date format. $future_date = date('m/d/Y',$future_date); echo $future_date; Today is September 1st and the value of $future_date is October 1st because there are 30 days in Spetember. Now you can compare your dates. if ($nexteval $future_date) { echo This date has already passed; } else { *do some processing* } Hope that helps. Dan
Re: [PHP] Date +30 comparison
i prefer http://in3.php.net/strtotime it supports loads of other formats as well (including +30 days and 8/26/2009) above all it returns unix time stamp which can be used directly with date(). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date +30 comparison
At 5:43 PM +0100 9/1/09, Ashley Sheridan wrote: On Tue, 2009-09-01 at 12:19 -0400, David Stoltz wrote: I'm really struggling with dates in PHP. (Yes, I tried reading the manual)... Can someone provide code that does this: Takes current date, assigns it to a variable (let's say $today) Then adds 30 days to $today variable Takes a string ($nexteval) like '8/26/2009' and compare it to $today. The variable $nexteval must be greater than $today (which is today + 30 days) or a message is echoed. I'm finding this difficult to do, but I'm coming from an ASP background. Any help appreciated. PHP (like all languages I know) treats dates as numbers; and PHP specifically uses seconds since January 1st 1970 (other languages sometimes have different start points and can measure in milliseconds instead). With this in mind, you can compare dates directly as you would an integer, and the later date will be the higher value. To add 30 days to a given date, you could use the date_add function (http://uk2.php.net/manual/en/datetime.add.php ) which has various formats you can use to add different time units. Lastly, to turn a date like 8/26/2009, I would probably try to break it down into it's component parts (maybe using explode('/', $string_date) ) and then using those values as arguments in a mktime() function. PHP should automatically treat the values as integers if they are strings, because like ASP, it uses loose typing on variables. David : First get the date to seconds, like so: $today_date = '8/26/2009'; $next_date = strtotime($today_date) + (86400 * 30); OR $next_date = strtotime('+30 days', strtotime($today_date)); Then take the seconds back to a date, like so: $the_date = date('m/d/Y', $next_date); Here's the example: http://www.webbytedd.com//future-date/ Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Date +30 comparison
Ok, this is how I finally managed to get it to work - I'm sure there are other ways, but this works: //Check to make sure the next eval date is more than 30 days away $d1 = date('Y-m-d', strtotime($todays_date . '+30 day')); $d2 = date('Y-m-d', strtotime($nextdate)); if($d1$d2){ echo Sorry, your next evaluation date must be at least 30 days away, Click BACK to continue.; exit; } -Original Message- From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk] Sent: Tuesday, September 01, 2009 12:43 PM To: David Stoltz Cc: php-general@lists.php.net Subject: Re: [PHP] Date +30 comparison On Tue, 2009-09-01 at 12:19 -0400, David Stoltz wrote: I'm really struggling with dates in PHP. (Yes, I tried reading the manual)... Can someone provide code that does this: Takes current date, assigns it to a variable (let's say $today) Then adds 30 days to $today variable Takes a string ($nexteval) like '8/26/2009' and compare it to $today. The variable $nexteval must be greater than $today (which is today + 30 days) or a message is echoed. I'm finding this difficult to do, but I'm coming from an ASP background. Any help appreciated. PHP (like all languages I know) treats dates as numbers; and PHP specifically uses seconds since January 1st 1970 (other languages sometimes have different start points and can measure in milliseconds instead). With this in mind, you can compare dates directly as you would an integer, and the later date will be the higher value. To add 30 days to a given date, you could use the date_add function (http://uk2.php.net/manual/en/datetime.add.php ) which has various formats you can use to add different time units. Lastly, to turn a date like 8/26/2009, I would probably try to break it down into it's component parts (maybe using explode('/', $string_date) ) and then using those values as arguments in a mktime() function. PHP should automatically treat the values as integers if they are strings, because like ASP, it uses loose typing on variables. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Date +30 comparison
At 1:28 PM -0400 9/1/09, David Stoltz wrote: Ok, this is how I finally managed to get it to work - I'm sure there are other ways, but this works: //Check to make sure the next eval date is more than 30 days away $d1 = date('Y-m-d', strtotime($todays_date . '+30 day')); $d2 = date('Y-m-d', strtotime($nextdate)); if($d1$d2){ echo Sorry, your next evaluation date must be at least 30 days away, Click BACK to continue.; exit; You got it. Just transform any date to seconds and then compare those seconds to other seconds (taken from other dates) and evaluate. Dead simple. Also note that the strtotime() and date() combination provides some very nice features like keeping track of the resultant date. You don't have to worry about leap years, or months having 28-31 days, or anything like that -- it's all taken care of for you, pretty neat huh? Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date +30 comparison
On Tue, Sep 1, 2009 at 1:27 PM, teddtedd.sperl...@gmail.com wrote: First get the date to seconds, like so: $today_date = '8/26/2009'; $next_date = strtotime($today_date) + (86400 * 30); No. Due to Daylight Saving Time, many time zones have two days each year when the number of seconds in a day is not 86400. OR $next_date = strtotime('+30 days', strtotime($today_date)); Then take the seconds back to a date, like so: $the_date = date('m/d/Y', $next_date); Yes. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date +30 comparison
On Tue, Sep 01, 2009 at 02:47:43PM -0400, Andrew Ballard wrote: On Tue, Sep 1, 2009 at 1:27 PM, teddtedd.sperl...@gmail.com wrote: First get the date to seconds, like so: $today_date = '8/26/2009'; $next_date = strtotime($today_date) + (86400 * 30); No. Due to Daylight Saving Time, many time zones have two days each year when the number of seconds in a day is not 86400. This and the 2038 bug are reasons to do this type of calculation with Julian days, as opposed to seconds. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date +30 comparison
At 2:47 PM -0400 9/1/09, Andrew Ballard wrote: On Tue, Sep 1, 2009 at 1:27 PM, teddtedd.sperl...@gmail.com wrote: First get the date to seconds, like so: $today_date = '8/26/2009'; $next_date = strtotime($today_date) + (86400 * 30); No. Due to Daylight Saving Time, many time zones have two days each year when the number of seconds in a day is not 86400. Arrggg. But good to know. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php