Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - add 1 day to date format mm-dd-yyyy

Tags:

date

php

<?php     $date = "04-15-2013";     $date = strtotime($date);     $date = strtotime("+1 day", $date);     echo date('m-d-Y', $date); ?> 

This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970.

The $date will be coming from a POST in the format m-d-Y, I need to add one day and have it as a new variable to be used later.

Do I have to convert $date to Y-m-d, add 1 day, then convert back to m-d-Y? Would I be better off learning how to use DateTime?

like image 321
Jayr Avatar asked Apr 15 '13 15:04

Jayr


People also ask

How can increase day in date in PHP?

You can use strtotime. $your_date = strtotime("1 day", strtotime("2016-08-24")); $new_date = date("Y-m-d", $your_date);

How do you add a day in Strtotime?

echo date ( 'Y-m-d' , strtotime ( $Date . ' + 10 days' )); ?> Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds.

How can get date in dd-mm-yyyy format in PHP?

Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.


1 Answers

there you go

$date = "04-15-2013"; $date1 = str_replace('-', '/', $date); $tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));  echo $tomorrow; 

this will output

04-16-2013 

Documentation for both function
date
strtotime

like image 180
Fabio Avatar answered Oct 20 '22 10:10

Fabio