1

This might be a small issue for many but I am not able to solve it. I am trying to convert date stored in my db and extract day, month and year separately. I have been trying with the following code for conversion and it is returning the value depicting 01 January 1970 for what ever the input date value. $item_date_day = date('d M Y', strtotime($item_date)); where $item_date is the value from db

Sample DB Value: 28 September 15 (Monday)

Returned date after conversion: 01 Jan 1970

Required output: 28 Sep 2015

Any clue why the conversion is going wrong?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
krishna89
  • 850
  • 1
  • 19
  • 42

3 Answers3

4

strtotime does not accept this format. Actually the formats accepted by this function is limited. Try this instead:

<?php

    $date = DateTime::createFromFormat('d F y (l)', $item_date);
    $item_date_day = $date->format('d M Y');
Ahmad
  • 5,551
  • 8
  • 41
  • 57
1

This is one possible solution, this way you can make sure that not matter what is the last item it will always return the right.

 $date_str = '28 September 15 (Monday)';
 $date_arr = explode(' ', $date_str);
 $date_clean = array_pop($date_arr);
 $date = implode($date_arr);
 echo date('d M Y', strtotime($date));

This will output

28 Sep 2015
0

The day name in brackets is stopping strtotime from working. You could remove it using a regex. Example:

$date = '28 September 15 (Monday)';
$date = preg_replace('/(.*) (\(.*\))/', '$1', $date);
echo date('d M Y', strtotime($date));
GluePear
  • 7,244
  • 20
  • 67
  • 120