Reputation: 2315
I need to convert date string into number:
2012-Sep-01 to 2012-09-01
Any idea? Regards,
Upvotes: 0
Views: 5574
Reputation: 9
Hi, try using this in PHP and you will surely get what you want, I was searching for the same but didn't found but later discovered I had the date-time formula used somewhere and modified it to get this.
$getdatetime=date_create('2020-May-21');
$dateconverted=date_format($getdatetime,'Y-m-d');
echo $dateconverted;
Upvotes: 0
Reputation: 2570
Try to use DateTime PHP class:
$date = "2012-Sep-01";
$result = DateTime::createFromFormat("Y-M-d", $date)->format("Y-m-d");
Upvotes: 6
Reputation: 191729
$date = DateTime::createFromFormat('Y-M-j', '2012-Sep-01');
echo $date->format('Y-m-d');
Actually stolen from the manual: http://us.php.net/manual/en/datetime.createfromformat.php
Upvotes: 3
Reputation: 33502
This might work for you:
echo date("Y-m-d", strtotime($yourCurrentDateVat))'
PHP website is down at the moment, but here is some extra info on the date function.
Upvotes: 6
Reputation: 5777
$timestamp = strtotime('22-09-2008');
$new_date = date("y-m-d", $timestamp);
Upvotes: 1
Reputation: 3773
You should use strtotime()
. More info on here. And then date()
to create a new string. More info on that here.
$date = '2012-Sep-01'; //Sets your date string
$time_epoch = strtotime($date); //Converts the string into an epoch time
$new_date = date('Y-m-d', $time_epoch); //Creates a new string
Upvotes: 1
Reputation: 39704
Use strtotime()
:
echo date('Y-m-d', strtotime('2012-Sep-01'));
Upvotes: 4