How can I convert this string 05/Feb/2010:14:00:01
to unixtime ?
Asked
Active
Viewed 1.8e+01k times
15

hakre
- 193,403
- 52
- 435
- 836

streetparade
- 32,000
- 37
- 101
- 123
9 Answers
54
Use the strtotime
function:
Example:
$date = "25 december 2009";
$my_date = date('m/d/y', strtotime($date));
echo $my_date;

Sarfraz
- 377,238
- 77
- 533
- 578
-
@streetparade: i mean for example you can not pass to it all sorts of strings for the date eg "28 of the December two thousand ten" – Sarfraz Feb 08 '10 at 16:10
-
It's worth noting strtotime also supports key words and phrases like 'yesterday' or 'this day next week' ;p – Rowan Feb 08 '10 at 17:34
20
For PHP 5.3 this should work. You may need to fiddle with passing $dateInfo['is_dst'], wasn't working for me anyhow.
$date = '05/Feb/2010:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);
Versions prior, this should work.
$date = '05/Feb/2010:14:00:01';
$format = '@^(?P<day>\d{2})/(?P<month>[A-Z][a-z]{2})/(?P<year>\d{4}):(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})$@';
preg_match($format, $date, $dateInfo);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
date('n', strtotime($dateInfo['month'])), $dateInfo['day'], $dateInfo['year'],
date('I')
);
You may not like regular expressions. You could annotate it, of course, but not everyone likes that either. So, this is an alternative.
$day = $date[0].$date[1];
$month = date('n', strtotime($date[3].$date[4].$date[5]));
$year = $date[7].$date[8].$date[9].$date[10];
$hour = $date[12].$date[13];
$minute = $date[15].$date[16];
$second = $date[18].$date[19];
Or substr, or explode, whatever you wish to parse that string.

erisco
- 14,154
- 2
- 40
- 45
-
It is not working at all. It says date_parse_from_format is un-defined. – Shahid Karimi Aug 26 '11 at 06:53
4
You should look into the strtotime() function.

Johannes Gorset
- 8,715
- 4
- 36
- 34
-
Your string might not be compatible with strtotime(); you might have to format it a little first. – Johannes Gorset Feb 08 '10 at 16:16
2
$d="05/Feb/2010:14:00:01";
$dr= date_create_from_format('d/M/Y:H:i:s', $d);
echo $dr->format('Y-m-d H:i:s');
here you get date string, give format specifier in ->format() according to format needed

Dashrath
- 2,141
- 1
- 28
- 33
1
Simple exploding should do the trick:
$monthNamesToInt = array('Jan'=>1,'Feb'=>2, 'Mar'=>3 /*, [...]*/ );
$datetime = '05/Feb/2010:14:00:01';
list($date,$hour,$minute,$second) = explode(':',$datetime);
list($day,$month,$year) = explode('/',$date);
$unixtime = mktime((int)$hour, (int)$minute, (int)$second, $monthNamesToInt[$month], (int)$day, (int)$year);

dev-null-dweller
- 29,274
- 3
- 65
- 85
0
If it's a string that you trust meaning that you have checked it before hand then the following would also work.
$date = new DateTime('2015-03-27');

Rick
- 12,606
- 2
- 43
- 41
0
Try this:
$new_date=date('d-m-Y', strtotime($date));

Jason Plank
- 2,336
- 5
- 31
- 40

enor
- 39
- 1
-
This is downvoted due to redundancy. The answer itself is correct, but was previously provided on Feb 8 by Sarfraz. – a coder Sep 20 '12 at 15:21