-3

I'am searching a lot without finding a solution.

Here is my code:

Post="2/28/2013"; 
$timestamp = strtotime($_POST['date']);
$datum = date("Y-m-d H:i:s", $timestamp);

I would like to have the next two Saturdays following a date (named by user) to provide two auxiliary dates to customers. I take the date to look in mySQL to see if the date is available.

This gives the next Saturday from today, but it did not help:

$nextSaturday= date("M d Y", strtotime('+1 Saturday'));
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
hamburger
  • 1,339
  • 4
  • 20
  • 41

4 Answers4

2

The nicest way to do this is with the PHP DateTime classes:

$date = new DateTime($_POST['date']);
$date->modify('next Saturday');

echo $date->format('M d Y');
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0
$time = time();
$saturdaysCount = 2;
$saturdays = [];
for ($i = 0; $i < $saturdaysCount; $i++){
    $time = strtotime("next Saturday", $time);
    $saturdays[] = date('Y.m.d', $time);
}

var_dump($saturdays);
DBuTbKa
  • 106
  • 5
0

The DateTime classes are definitely the way to go:-

$date = \DateTime::createFromFormat("d-m-Y", $_POST['date']);
$int = new \DateInterval('P1W');
$date->modify('next saturday');
$firstSat = $date->format('d-m-Y');
$date->add($int);
$secondSat = $date->format('d-m-Y');
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • sorry got an error: Call to undefined method DateTime::createFromForm – hamburger Jun 22 '13 at 16:07
  • Your version of PHP does not have DateTime::createFromFormat(). I recommend updating if you can. Otherwise you may, or may not, be interested in this question http://stackoverflow.com/q/17076627/212940 – vascowhite Jun 27 '13 at 12:07
-1
$nextSaturday= date("M d Y", strtotime($datum.' next saturday'));
$nextSaturday2= date("M d Y", strtotime($nextSaturday.'+ 1 week'));

http://php.net/manual/en/function.strtotime.php

amigura
  • 541
  • 3
  • 6