converting to YYYYMMDD format with the value date posted from PHP
I have a date value posted from PHP using JQuery (MMDDYYYY
) and I want to convert this date format to YYYYMMDD
using PHP.
$fromDate=str_replace("'","''",trim($_POST['p']));
echo "-->".$year = substr($fromDate, 6, 4)."<br>";
echo "-->".$month = substr($fromDate, 0, 2)."<br>";
echo "-->".$date = substr($fromD开发者_JS百科ate, 3, 2)."<br>";
echo $new_date = date( 'Ymd', strtotime($month, $date, $year ));
Suppose in the above code I have entered the date as 070122010
.The new_date
in the last line gives me 20100714
. I don't know why it is giving today's date. I have tried both mktime
and strtotime
, but both are giving me the same result. The desired result is 20100712
(YYYYMMDD
).
The DateTime class DateTime::createFromFormat function will do what you want, providing you have a PHP 5.3.0 or greater:
$date = DateTime::createFromFormat('mdY', '12312010');
echo $date->format('Ymd');
// 20101231
echo $date->format('Y-m-d');
// 2010-12-31
echo $date->format('Y-M-d');
//2010-Dec-31
strtotime($old_date)
won't work because MMDDYYYY
is not a valid date string: http://www.php.net/manual/en/datetime.formats.date.php
preg_replace("/([0-9]{2})([0-9]{2})([0-9]{4})/","$3$1$2",$old_date);
or an even shorter version:
preg_replace("/([0-9]{4})([0-9]{4})/","$2$1",$old_date);
strtotime
won't work because MMDDYYYY
is not a valid date string:
preg_replace("/([0-9]{2})([0-9]{2})([0-9]{4})/","$3$1$2", $orig_date);
You don't need to tear apart the date. All you need to do is move the year to the front.
$orig = '01022010';
$new = substr($orig,4).substr($orig,0,4);
Here are two ways:
$d = preg_replace("/([0-9]{4})([0-9]{4})/", "$2$1", $originalDate);
or
$d = substr($originalDate, 4, 4) . substr($originalDate, 0, 4);
If one of these makes more sense to you, I suggest you use that one. Otherwise, I suspect the second would be slightly faster though I haven't tested.
function mmddyyyy_to_yyyymmdd ($input) {
$monthdays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if ( !preg_match('/\\A\\d{8}\\Z/', $input) ) {
return false;
}
$month = (int)substr($input, 0, 2);
$day = (int)substr($input, 2, 2);
$year = (int)substr($input, 4);
if ( $year % 4 == 0 and
( $year % 100 != 0 or $year % 400 == 0 )
) {
$monthdays[1] = 29;
}
if ( $month < 1 or
$month > 12 or
$day < 1 or
$day > $monthdays[$month-1]
) {
return false;
}
if ( $month < 10 ) { $month = '0'.$month; }
if ( $day < 10 ) { $day = '0'.$day; }
while ( strlen($year) < 4 ) {
$year = '0'.$year;
}
return $year.$month.$date;
}
$new_date = date("Ymd", strtotime($old_date));
$data = strptime('12242010','%m%d%Y');
$date = mktime(
$data['tm_hour'],
$data['tm_min'],
$data['tm_sec'],
$data['tm_mon']+1,
$data['tm_mday'],
$data['tm_year']+1900);
echo date('Ymd',$date);
Using substr would be faster: http://php.net/manual/de/function.substr.php
$orig="20072010"; // DDMMYYYY
$new=substr($orig,5,4)+substr($orig,3,2)+substr($orig,0,2); // YYYYMMDD
And it is worth reading this: http://www.php.net/manual/en/datetime.formats.date.php
This is my answer. At last I succeeded.
date( 'Ymd', mktime(0,0,0,$month, $date, $year ));
精彩评论