Forum Moderators: coopster

Message Too Old, No Replies

Time / Date problem

         

tsecher

9:19 pm on Feb 28, 2006 (gmt 0)



$born = '2006-01-17';

$kill = date('Y-m-d',mktime(0, 0, 0, date("m",$born), date("d",$born)+180, date("Y",$born)));

echo $kill;

Why doen't this work? I want the code to calculate a given date ($born) + 180 days.

whoisgregg

10:24 pm on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



date() [php.net] expects a timestamp, not a string. Check out strtotime() [php.net] as one way to convert your string date into a timestamp.

adb64

10:25 pm on Feb 28, 2006 (gmt 0)

10+ Year Member



Use strtotime:

Get the unixtime for born and add 180 times # seconds in a day:

$born = '2006-01-17';
$kill = date('Y-m-d',strtotime($born) + (180 * 86400));
echo $kill;

Check strtotime function what the exact string format should look like.

coopster

1:07 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, tsecher.

I see that you figured out that it is possible to use date() and mktime() together to find dates in the future. As the others have noted, you need to pass this function a timestamp and there are a number of ways to accomplish the task at hand. Another common method is to extract the pieces you need:

$born = '2006-01-17'; 
list($year, $month, $day) = explode [php.net]('-', $born);
$kill = date('Y-m-d', mktime(0, 0, 0, $month, $day + 180, $year));
echo $kill;

Another member recently asked a similar question -- this thread may interest you, especially if that $born variable value is coming from a database:
[webmasterworld.com...]