2

I' am trying to convert the date of next 7 days into timestamp so that I can compare against my date timestamp in database to get some results.

This function is used to get the next 7 days from today

$next_date = date("d/m/Y", strtotime("7 day"))

Output

30/04/2014

Now I' am again running strtotime() on $next_date variable who holds the next 7days and converting to timestamp.

echo strtotime($next_date);

This is not working. I followed this stackoverflow answer and few others.

Community
  • 1
  • 1
Navneil Naicker
  • 3,586
  • 2
  • 21
  • 31

2 Answers2

3

As an alternative suggestion you could look at PHP's internal DateTime() and DateInterval() classes. It makes it a bit easier to convert between formats and do date/time addition and subtraction imho. DateInterval requires at least PHP version 5.3.

An example:

// create a current DateTime
$currDate = new DateTime();

// copy the current DateTime and
// add an interval of 7 days
$nextDate = clone $currDate;
$nextDate->add(new DateInterval('P7D'));

// both objects are easily converted to timestamps
echo $currDate->getTimestamp(); // e.g: 1398296728
echo $nextDate->getTimestamp(); // e.g: 1398901528

// and both can be easily formatted in other formats
echo $currDate->format('d/m/Y'); // e.g: 24/04/2014
echo $nextDate->format('d/m/Y'); // e.g: 01/05/2014

EDIT

For completeness, here's another example of how you can add seven days to a DateTime object:

$now  = new DateTimeImmutable();
$then = $now->modify('+7 days');

var_dump($now->format('Y-m-d'), $then->format('Y-m-d'));

Yields:

string(10) "2016-05-24"
string(10) "2016-05-31"

You can also use DateTime - the difference in this use case is that DateTime::modify() will modify the instance $now where DateTimeImmutable::modify() will return a new DateTimeImmutable object - so if you need to create a new object whilst retaining the old one, it's probably the most succinct approach.

Hope this helps :)

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
2

Just store the value from strtotime first?

$timestamp_in_7_days = strtotime('7 day');
$next_date = date('d/m/Y', $timestamp_in_7_days);

There is no need to throw the time back and forth between unix timestamp and date-format.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • Indeed. I just wanted to add, the poster said "it did not work". I'm guessing the problem is that you made $next_date formatted d/m/Y, and according to PHP's manual: `Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.` – Kai Apr 23 '14 at 23:35
  • @Kai - Yes, that is correct. He could make it work by splitting up the string, using mktime or something, but this is the cleanest approach as I see it ;) – OptimusCrime Apr 23 '14 at 23:37
  • Yep, I would have said that approach myself if you hadn't beaten me to it. =) – Kai Apr 23 '14 at 23:38