Best way to create future PHP dates

Is there a faster way to create a date, for example:

echo date('Ym-d', mktime(0, 0, 0, date("m"), date("d")+3, date("Y"))); 

Thanks if you can help.

+4
source share
2 answers

How about strtotime () :

 date('Ym-d', strtotime('+3 days')); 
+11
source

You will need to learn strtotime () . I would suggest that your last code would look something like this:

 $currentDate = strtotime('today');//your date variable goes here $futureDate = date('Ym-d', strtotime('+ 2 days', $currentDate)); echo $futureDate; 

Live demo

If you are using PHP version> = 5.2, I highly recommend that you use the new DateTime object. For example, as shown below:

 $futureDate = new DateTime("today"); $futureDate->modify("+2 days"); echo $futureDate->format("Ymd"); 

Live demo

0
source

Source: https://habr.com/ru/post/1307090/


All Articles