-2

I can get the for example 19 March of specific date with this code:

$date = strtotime(" 19 March", $current_time);

For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011. How can I do that?

Shadi Hariri
  • 197
  • 16
  • Google the `strtotime()` function, it's all on the php page. – Epodax Dec 23 '15 at 08:54
  • I've already read the strtotime(). I couldn't find anything for my problem. Maybe it didn't catch my eye. So If you have any resource I would be happy to have it. But right now this comment doesn't help me. @Epodax – Shadi Hariri Dec 23 '15 at 09:54

4 Answers4

4

Using PHP DateTime this can be achieved as follows:

// New DateTime object
$date = new DateTime('2010-03-19');

// Add a year
$date->add(new DateInterval('P1Y'));

// Output timestamp
echo $date->getTimestamp();
Peter
  • 8,776
  • 6
  • 62
  • 95
1

You can do something like as

$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));

if(strtotime($given_date) - strtotime($date_month) < 0){
    echo date('l,d F Y',strtotime("$get $year"));
}else{
    echo date('l,d F Y',strtotime("$get ".($year+1)));
}
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • I assume one of the $given_date is $get? Also I liked to get the answer with just using strtotime, I guess there is not any way. – Shadi Hariri Dec 23 '15 at 09:51
0

You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.

//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
Parixit
  • 3,829
  • 3
  • 37
  • 61
0

You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions

$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();

$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();
Omar
  • 159
  • 4
  • 5