2

我正在尝试从以下日期格式中获取开始日期和结束日期

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]));

谁能帮我吗?

4

5 回答 5

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]));
于 2013-05-10T06:55:58.903 回答
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]));
于 2013-05-10T06:03:57.063 回答
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";

希望这很有用。

于 2013-05-10T07:08:13.363 回答
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但是,如果您在-string 中添加一些内容,它将不起作用,例如$date = 'Wed 19 2013 - Sat 22 June 2013';

于 2013-05-10T08:48:17.183 回答
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
于 2013-05-10T08:59:35.057 回答