-1

trying to have a basic time stamp next to my posts on a cms but strtotime is giving me issues.

So i have this:

$postDate = $row['date'];
$written_month = date("F", strtotime($postDate));
$short_month = substr($written_month, 0, 3);
$day_number = date("j", strtotime($postDate));

echo '
    <div id="postTimeStamp">
    <div id="postTimeStamp_day">
    '.$day_number.'
    </div>

    <div id="postTimeStamp_month">
    '.$short_month.'
    </div>
    </div>
';

I cant figure out why but the day is always the 1st and the month is january.

If i do

     $echo $postDate;

It shows the dat correctly.

In the DB the date is stored as e.g. 10/12/2013.

Where am i going wrong?

Craig.

Lovelock
  • 7,689
  • 19
  • 86
  • 186
  • What's the expected output ? Also there's a ton of already asked and answered php/date questions. You may take a look at them. – HamZa Nov 29 '13 at 00:32
  • $day_number should be 10, and $short month should be dec. This according to the date example. it works in terms of giving a day and month. But always shows 1 - jan and not the date stored in the database – Lovelock Nov 29 '13 at 00:34
  • 1
    What's the output of `var_dump($row['date']);`? – George Brighton Nov 29 '13 at 00:34

3 Answers3

1

I tried your code and its working fine here:

$postDate = '12/12/2012';
$written_month = date("F", strtotime($postDate));
$short_month = substr($written_month, 0, 3);
$day_number = date("j", strtotime($postDate));

echo '
    <div id="postTimeStamp">
    <div id="postTimeStamp_day">
    '.$day_number.'
    </div>

    <div id="postTimeStamp_month">
    '.$short_month.'
    </div>
    </div>
';

Make sure $row['date']; is really a string and in a supported format by strtotime (http://www.php.net/manual/de/datetime.formats.date.php).

Chris
  • 1,226
  • 1
  • 12
  • 26
1

The date 10/12/2013 is ambiguous. In the US it means October 12 2013, and in the UK it means December 10 2013. I'm sure MySQL would never return a date value in this format, so I think your main problem is that you're using TEXT or VARCHAR values to store dates in your database. Don't do that.

Also, making two calls to strtotime() is a bit redundant, and if you want a three-letter month, use 'M' in the strtotime() call. Try this:

list($month,$day)=explode(' ',date('M j',strtotime('2013-10-12')));
r3mainer
  • 23,981
  • 3
  • 51
  • 88
0

i tested a different format so d-m-y instead of d/m/y and that works fine.

No idea why but hey ho :)

Lovelock
  • 7,689
  • 19
  • 86
  • 186