-2

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.

Sougata Bose
  • 30,871
  • 8
  • 44
  • 87
Volkan
  • 47
  • 10

2 Answers2

0
$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
iLaYa ツ
  • 3,887
  • 3
  • 27
  • 45
0

@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

Robert
  • 18,927
  • 5
  • 54
  • 81