svk
svk

Reputation: 4553

How can I find a date after certain weeks?

I need to find a date after a certain number of weeks. For example, if the start date is Saturday, 31 October 2009, and if I choose 16 weeks then I need to find the date after sixteen Saturdays.

Thanks in advance.

Upvotes: 1

Views: 391

Answers (4)

Jason
Jason

Reputation: 2605

If you're using PHP 5.2.0 or later, you can use the DateTime class

<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.

// Now add 16 weeks
$date->modify('+16 weeks');

// Now you can output it however you wish
echo $date->format('Y-m-d');
?>

Upvotes: 1

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827684

You can use strtotime:

// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20


// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07

Upvotes: 5

artlung
artlung

Reputation: 33823

strtotime() is what you want: it takes an expression and turns it into a date object. Also see date():

$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)

Upvotes: 2

James Black
James Black

Reputation: 41858

Why not just use unix time, subtract 7 * 24 * 3600 * 16 from that and then convert that back into the date that you need.

This will help with the conversion back: http://php.net/manual/en/function.date.php

Upvotes: 0

Related Questions