你好请帮我把字符串转换成日期: - 谷歌搜索了这个,但没有得到理想的结果
<?php
$todaydate= $_GET['date']; //sending 201213 ie index.php?date=201213
$todaydate=preg_replace("/[^0-9,.]/", "", $todaydate);
echo $todaydate."----------"; //prints 201213
$todaydate = date("ymd", strtotime($todaydate));
echo $todaydate."----------"; // i want to print 131220 here but it prints today's date
?>
答案 0 :(得分:1)
尝试使用mktime:
<?php
$todaydate = $_GET['date']; //sending 201213 ie index.php?date=201213
$todaydate = preg_replace("/[^0-9,.]/", "", $todaydate);
echo $todaydate."----------"; //prints 201213
$d = substr($todaydate, 0, 2);
$m = substr($todaydate, 2, 2);
$y = substr($todaydate, 4, 2);
$t = mktime (0, 0, 0, $m, $d, $y);
$todaydate = date("ymd", $t);
echo $todaydate."----------"; // i want to print 131220 here but it prints today's date
?>
答案 1 :(得分:1)
更简单的解决方案:
<?php
$todaydate = $_GET['date']; //sending 201213 ie index.php?date=201213
$todaydate = preg_replace("/[^0-9,.]/", "", $todaydate);
$date = DateTime::createFromFormat('dmy', $todaydate);
echo $date->format('ymd');
?>
使用此代码,您可以使用任何格式显示日期,因为您现在拥有有效日期。