我正在尝试从以下日期格式
获取开始日期和结束日期Wed 19 - Sat 22 June 2013
现在我想要的输出是: -
Startdate = 2013-06-19
Enddate = 2013-06-22
但我得到的输出为: -
Startdate = 1970-01-01
Enddate = 2013-06-22
我尝试过: -
$old_date = explode('-',$date);
$startdate = date("Y-m-d",strtotime($old_date[0]));
$enddate = date("Y-m-d",strtotime($old_date[1]));
任何人都可以帮助我吗?
答案 0 :(得分:3)
试试这个:
$date = "Wed 19 - Sat 22 June 2013";
$old_date = explode('-',$date);
$date = explode(' ', trim($old_date[1])); //updated line
$startdate = date("Y-m-d",strtotime($old_date[0].' '.$date[2].' '.$date[3]));
$enddate = date("Y-m-d",strtotime($old_date[1]));
答案 1 :(得分:2)
这样做:
$date= "Wed 19 - Sat 22 June 2013";
$old_date = explode(' - ',$date);
$Xdate=explode(" ",$old_date[1]);
$startdate = date("Y-m-d",strtotime($old_date[0]." ".$Xdate[2]." ".$Xdate[3]));
$enddate = date("Y-m-d",strtotime($old_date[1]));
答案 2 :(得分:1)
首先需要拆分日期字符串的两个部分。
$date= "Wed 19 - Sat 22 June 2013";
//split dates into array
$dates_array = explode(' - ',$date);
//get end date which is complete
list($EndDate, $Year) = explode(',', date('Y-m-d,Y', strtotime($dates_array[1])));
//Build start date
$StartDateStr = $dates_array[0] . $Year;
//Convert
$StartDate = date('Y-m-d', strtotime($StartDateStr));
echo "StartDate = $StartDate";
echo "EndDate = $EndDate";
希望这很有用。
答案 3 :(得分:1)
我认为你可以通过多种方式实现这一目标。这是一种做法。
<?php
$date = 'Wed 19 - Sat 22 June 2013';
$old_date = explode(' ',$date);
$year = $old_date[6];
$month = $old_date[5];
$firstDay = $old_date[1];
$secondDay = $old_date[4];
$yearAndMonth = $year . '-' . date('m', strtotime($month)) . '-';
$startDate = $yearAndMonth . $firstDay;
$endDate = $yearAndMonth . $secondDay;
echo $startDate . '<br />' . $endDate;
?>
<强>输出:强>
2013-06-19
2013-06-22
我相信这个例子给出了相当好的可读性。如果您向$date
- 字符串添加内容,例如$date = 'Wed 19 2013 - Sat 22 June 2013';
答案 4 :(得分:1)
<?php
$sDateString = 'Wed 19 - Sat 22 June 2013';
$sStartDay = preg_replace("/^([a-zA-Z]{3,4} [0-9]{2}).+/", "$1", $sDateString);
$sEndDay = preg_replace("/.+ \- ([a-zA-Z]{3,4} [0-9]{2}).+/", "$1", $sDateString);
$sMonth = preg_replace("/.+ ([a-zA-Z]+ [0-9]{4})$/", "$1", $sDateString);
$sStartDate = date("Y-m-d", strtotime($sStartDay . ' ' . $sMonth));
$sEndDate = date("Y-m-d", strtotime($sEndDay . ' ' . $sMonth));
echo("<p>" . $sStartDate . "</p>");
echo("<p>" . $sEndDate . "</p>");
输出:
2013-06-19
2013-06-22