View Full Version : issues with strtotime and date functions..

02-22-2007, 04:28 AM
I'm trying to add 3 days to a stored starting date:

echo $_SESSION['start_date']."<br>";

$new_timestamp = ($_SESSION['start_date'] + strtotime("+3 days"));
$new_date = date('Y-m-d', $new_timestamp);

echo $new_date;

This ouputs:

2007-02-25 (which is clearly + 7 days?)

I would expect it to output:


Am I missing something?

02-22-2007, 06:36 AM
The result of strtotime() is a unix timestamp. You need to convert the your $_SESSION['start_date'] variable to a unix timestamp with mktime() before adding the strtotime() result to it.

02-22-2007, 08:23 PM
Thanks, that makes sense.

I've amended the code but it still doesn't seem to work:

echo $_SESSION['start_date']."<br>";

$start_time = mktime($_SESSION['start_date']);
$start_date = $start_time + strtotime("+3 days");
$new_date = date('Y-m-d', $start_date);

echo $new_date;



02-22-2007, 08:53 PM
Fumi, do you (or anyone else that reads this) know of a site that explains how to do stuff with the date in PHP? I'm sure I'm not the only one who's confused on this. I've had to work with mktime() and date() and such in the past, but I still don't understand it enough to work with it. I know that mktime() deals with seconds, but I don't know how to change this into a readable date. Thanks for any help you all can provide.

02-22-2007, 09:12 PM
Welll..... I can't really put it any better than the manual... it's all about boiling down a date or time to a common denominator. mktime() returns the number of seconds elapsed between January 1 1970 (Unix epoch) and the values you plug into the function.

Once you boil date/times down to that same common denominator, you can perform addition and subtraction on the value and then convert that mktime value back to a readable date and time using the date() and time() functions.

Quadrant6 you must pass mktime() (http://us2.php.net/manual/en/function.mktime.php) the month, day and year values separately.

//assuming date format of YYYY-MM-DD
$start_time = mktime(12, 0, 0, substr($_SESSION['start_date'], 5, 2), substr($_SESSION['start_date'], 8, 2), substr($_SESSION['start_date'], 0, 4));

Note the hour of 12 noon, to avoid issues with time zones, though it may not avoid the issues entirely.

02-22-2007, 09:17 PM
You want something like:

$time_a = '2006-07-01';

$time_b = date('Y-m-d',strtotime('+ 3 days',strtotime($time_a)));

echo $time_b;


YYYY-MM-DD is an acceptable input to strtotime, so you don't need mktime (which takes separate arguments for each portion of the date/time), and the 2nd argument to strtotime can be a timestamp to start from, rather than defaulting to now.

the above split up slightly for clarity:

$time_a = '2006-07-01'; //start time

$time_a_as_timestamp = strtotime($time_a); //turn it into a ts

$time_b_as_timestamp = strtotime('+3 days',$time_a_as_timestamp); // add 3 days to time_a

$time_b = date('Y-m-d',$time_b_as_timestamp); //then format

02-22-2007, 11:10 PM
Thanks - that works perfectly.

As a side note: I'm using it later in an If comparison to check whether the current row date is newer than this $new_date

while($row = mysql_fetch..

if($row['date'] >= $new_date){

Which for example does: if '2006-02-13' >= '2006-03-10'

I'm surprised that this comparison seems to work? I thought I'd have to convert both again to timestamps or numbers but PHP just knows how to handle (though I'm posting this just in case I'm wrong and missing something :) )