Reputation: 265
How would I change Following strings into specific customized format using php.
CHANGE
11/16/2012
11-16-2012
To
2012-16-11
Upvotes: 2
Views: 1593
Reputation: 1076
$oldDate = '11/16/2012';
$newDate = date('Y-d-m',strtotime($date1));
Use combinations here to get exact output format: http://php.net/manual/en/function.date.php
Upvotes: 0
Reputation: 110
$date = strtodate(11-16-2012);
$ans = date('Y-d-Y',$date);
echo $ans;
Upvotes: 0
Reputation: 12525
You can use class DateTime
to make it easier working with dates.
Here's sample code for your input/output:
<?php
$date = new DateTime('11/16/2012');
echo $date->format('Y-d-Y');
Edit: you can use following regex pattern \d+/\d+/\d+
to extract date from long string.
Upvotes: 1
Reputation: 132138
Not sure if you are asking how to get Year-Month-Year
, or how to handle either hyphens or slashes, so the funkyDateString
function does both:
function funkyDateString($date) {
return date("Y-m-Y", strtotime(preg_replace('#-#', '/', $date)));
}
echo funkyDateString("11/16/2012");
echo funkyDateString("11-16-2012");
If you need the date formatted differently, then look up the formatting options in php's date docs
Upvotes: 0