Reputation: 73
I am trying to fetch the Start date and end date from the following date format
Wed 19 - Sat 22 June 2013
Now what i want the output is :-
Startdate = 2013-06-19
Enddate = 2013-06-22
But i am getting the output as:-
Startdate = 1970-01-01
Enddate = 2013-06-22
What i have tried:-
$old_date = explode('-',$date);
$startdate = date("Y-m-d",strtotime($old_date[0]));
$enddate = date("Y-m-d",strtotime($old_date[1]));
Can anyone help me out?
Upvotes: 2
Views: 313
Reputation: 11213
You first need to split both parts of your date string.
$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";
Hope that's useful.
Upvotes: 1
Reputation: 5520
I think you can achieve this in many ways. Here's one way of doing it.
<?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;
?>
Output:
2013-06-19
2013-06-22
I believe this example gives rather good readabiliy. It would not work though if you add something to the $date
-string, like $date = 'Wed 19 2013 - Sat 22 June 2013';
Upvotes: 1
Reputation: 4483
<?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>");
Output:
2013-06-19
2013-06-22
Upvotes: 1
Reputation: 1007
Try this :
$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]));
Upvotes: 3
Reputation: 818
Do this:
$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]));
Upvotes: 2