1) it is necessary to display the current date in this format: 03/24/2016

2) still need to take such a date for example:

$date = '10.03.2016'; 

and add to it for example 30 days, which would be like this:

 echo $date; //Ответ: 09.04.2016 

Completely confused, I do not know how to do it all. Help me please.

  • echo date ("dmY"); // I figured out how to get the current date), but I don’t know the number of days in such a format as n. then I need to add the value of 10/23/2016 to the database as a string. and then remove it at any convenient time, plus a few days, and write it back. - PHPcoder

2 answers 2

You can format the date using the date () function, which takes the date and time as the second argument, if you do not specify, the current one will be

 echo date('dmY'); 

In order to add 10 days to the current date, you can use the function strtotime ()

 echo date('dmY', strtotime("+10 days")); 

If you need to add ten days to the line '21 .11.2015 ', you can take into account that strtotime () returns the time in seconds from January 1, 1970 and just add the number of seconds corresponding to 10 days

 echo date('dmY', strtotime('21.11.2015') + 10 * 24 * 3600); 
  • I now understand how to add to the current, thanks. And what if there is a line in the database, for example 11/21/2015, how to add these 10 days to such a line? The bottom line is what I got using date ('dmY'); I wanted to, and brought to the database this date. And after a few days, you have to get the line and it turns out to be plus 10 days - PHPcoder
  • one
    Translate the string into the date and add 10 days - Den
  • Please help to translate the string in the date, I will be very grateful. - PHPcoder
  • Corrected the answer. - cheops 5:09

To work with dates in PHP there is a built-in class DateTime . Dates can be displayed in any format , add intervals .

 date_default_timezone_set('Europe/Moscow'); $date = new DateTime('21.11.2015'); $date->add(new DateInterval('P30D')); // "P" от "period", "30D" 30 days (дней) echo $date->format('jmY') . "\n"; // выведет: 21.12.2015