Re: Date +30 comparison

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/bbbb/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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux