php - How to convert below string to date time -


i have different types of date strings, examples given below. how can convert these strings in date time in php. please me solve problem?

examples:

1: 10:12 etaug 31, 2009 2: august 31, 2009, 9:08 et 3: sept. 1, 2009 10:20 a.m. et  4: 9:45 etsep 2, 2009 

i had tried code result wrong.

<?php date                      = 'aug. 28, 2009 10:26 a.m. et';  $remove[] = "'"; $remove[] = '.'; $remove[] = ','; $remove[] = 'am'; $remove[] = 'et'; $replace = str_replace($remove, '', $date); $space[] = ' '; $datetime = str_replace($space, '-', $replace); echo $datetime . '<br>'; $date                      = date_create_from_format('f-d-y', $datetime); echo "format:" . $date->format('y-m-d') . "\n"; ?> 

i used below method , got answer.

$replacechar      = str_replace($remove, '', $date); $space[]          = ' '; $replacespace     = str_replace($space, '-', trim($replacechar)); $explodedate      = explode('-', $replacespace); $datetimeexplode  = $explodedate[0].'-'.$explodedate[1].'-'.$explodedate[2].' '.$explodedate[3]; if(strstr($explodedate[3], ":")){    $date_time       = date_create_from_format('f-d-y h:i', $datetimeexplode);    $datetimereplace = $date_time->format('y-m-d h:i');    echo $datetimereplace; } $datetimeexplodesecond = $explodedate[2].'-'.$explodedate[3].'-'.$explodedate[4].' '.$explodedate[0]; if(strstr($explodedate[0], ":")){    $date_time       = date_create_from_format('f-d-y h:i', $datetimeexplodesecond);    $datetimereplace = $date_time->format('y-m-d h:i');    echo $datetimereplace;                  } 

answer is:

2009-8-31 10:12 2009-8-31 9:08 2009-9-1 10:20 2009-9-2 9:45 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -