I want to fetch only date: 2013-09-24 from the following string using php:
$2013-09-24T11:53:58+02:00am09pam53119_Tue, 24 Sep 2013 11:53:58 +0200Europe/Berlin58000000Tuesday30[0]
I am getting above string from this line of code:
$second_date = date('$first_date[0]', strtotime("+15 days") );
Is there any php built-in function to do this or what can be the simplest way?
If you have that weird string, then just use this
<?php
$str = "$2013-09-24T11:53:58+02:00am09pam53119_Tue, 24 Sep 2013 11:53:58 +0200Europe/Berlin58000000Tuesday30[0]";
$pattern = '/[0-9]{4}-[0-9]{2}-[0-9]{2}/';
preg_match($pattern, $str, $matches);
?>
Use DateTime class. You can even fetch time and timezone :
$str = "$2013-09-24T11:53:58+02:00am09pam531.....";
$dt = new DateTime(substr($str, 1, 25));
echo $dt->format('Y-m-d');
# 2013-09-24
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-24 11:53:58
[timezone_type] => 1
[timezone] => +02:00
)
*/