0

I am trying to find the date from the database which should be exactly 30 days from now.

I want to retrieve the date from the database which is DateTime format. change this to timestamp with strtotime(). feed this to date() and retrieve the mand i and then do local calculation to find out if its less exactly thirty days.

I want to know if there is any straight forward and easy procedure to do this.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
azam khan
  • 51
  • 9
  • What DB is that? Mysql? – Yasen Zhelev Apr 12 '15 at 11:43
  • Everything in PHP is very well documented. Just take a look at the examples in the `strtotime()` documentation and you will find the answer in seconds: http://php.net/manual/en/function.strtotime.php#refsect1-function.strtotime-examples – Marc Apr 12 '15 at 11:44
  • possible duplicate of [Add number of days to a date](http://stackoverflow.com/questions/2332681/add-number-of-days-to-a-date) – Marc Apr 12 '15 at 11:45

3 Answers3

0
date('Y-m-d H:i:s', strtotime(' +30 day'));

this will also work

date('Y-m-d H:i:s', strtotime(' +1 month'));

or use any format you need, but this method will compute the next date plus 30 days

PHP strtotime

related answers:

  1. PHP: get next 13 dates from date?
  2. get next and previous day with php
Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • @azamkhan, probably you will have to covert the date into datetime format for use in DB (it depends on your date format used in DB), this example ujses the Y-m-d format, you can chnage this or even use http://php.net/manual/en/function.date-parse.php – Nikos M. Apr 12 '15 at 11:46
0

Try this :

$date_formatted = date('Y-m-d H:i:s', strtotime($val->time));

To retrieve the m or i or both, try this :

$v = date('Y-m-d H:i:s'); //test it with now.
$date_formatted = date('m i', strtotime($v));

For find date from database that exactly 30 days from now you can try this:

$date_formatted = date('Y-m-d H:i:s', strtotime(' +30 day'));
Eko Junaidi Salam
  • 1,663
  • 1
  • 18
  • 26
0

If you want to directly do that in the SQL you can do that.

WHERE DATE_ADD(NOW(), INTERVAL 1 MONTH) = your_date_column_here

This way you can directly get only dates that are 1 month ahead with your query.

Or if you want to get a UNIX timestamp of the date that is one month in advance from today with PHP use this

strtotime('+ 1 month');

Using '+1 mohth' instead of '+30 days' will always give you one month ahead, not just 30 days ahead.

Yasen Zhelev
  • 4,045
  • 3
  • 31
  • 56