I am getting a text datetime
by explode()
function. Like this.
$date = "26 July 2014";
I want to convert it to datetime
and i can do substractions.
I am getting a text datetime
by explode()
function. Like this.
$date = "26 July 2014";
I want to convert it to datetime
and i can do substractions.
$s = "26 July 2014";
$date = strtotime($s); //1406347200
echo date('m-d-Y H:i:s', $date); //07-26-2014 00:00:00
For Subtracting:
$newdate = strtotime ( '-2 day' , strtotime ( $s ) ) ;
$newdate = date ( 'Y-m-d' , $newdate );
echo $newdate; //2014-07-24
@Leerner answer is good but if you want to create DateTime object you can use
$dateTimeObj = DateTime::createFromFormat('d F Y', '26 July 2014');
or
$dateTimeObj = date_create_from_format('d F Y', '26 July 2014');
http://php.net/manual/en/datetime.createfromformat.php
to substract you can use modify or sub method
$dateTimeObj->modify('-1 day');
echo $dateTimeObj->format('d F Y');
or date_modify()
function
You can check how this work on this fiddle