<?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
?
You can use strtotime. $your_date = strtotime("1 day", strtotime("2016-08-24")); $new_date = date("Y-m-d", $your_date);
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With