0

can anyone explain why the following code

function convdate($date) { $formatdate = strtoupper(date('M', $date )); $formatdate .= " " .date('d',$date). " "; return $formatdate; } $sql = "SELECT * FROM livedates ORDER by date"; $res = mysql_query($sql); while($livedates = mysql_fetch_array($res)){ $output= convdate($livedates['date']) . "<br>"; echo $output; } 

Outputs :

DEC 31 DEC 31 DEC 31 

when if i just output the date from the resultset using

$output= $livedates['date']. "<br>"; 

I get

2013-08-03 2013-08-10 2013-12-09 

What I want to be getting is

AUG 03 AUG 10 DEC 09 

Its going to be a mistake on my part , I realise that but i'm stumped !

3
  • Is the date column defined as timestamp or as datetime or as bigint in DB? What does var_dump($livedates['date']) show? Commented Aug 28, 2013 at 17:36
  • @IvanHušnjak It looks like it's a DATE, according to the second output. Commented Aug 28, 2013 at 17:38
  • @IvanHušnjak as DATE in mysql ...just realised I need to be doing strtotime Commented Aug 28, 2013 at 17:39

4 Answers 4

2

You need to convert $livedates['date'] to a unix timestamp before passing it to the date function. strtotime can do it:

$formatdate = strtoupper(date('M', strtotime($date) )); 
Sign up to request clarification or add additional context in comments.

Comments

1

What is wrong is with your function

The second parameter of date has to be a timestamp so this is the function that would work

<?php function convdate($date) { $date = strtotime($date); $formatdate = strtoupper(date('M', $date )); $formatdate .= " " .date('d',$date). " "; return $formatdate; } ?> 

1 Comment

lol What to do when everyone answers the right answer ! Thanks all
0

Try changing your function to this:

function convdate($date) { $formatdate = new DateTime($date); return strtoupper($formatdate->format('M d')); } 

The DateTime class is amazingly wonderful to use, I suggest you check it out.

Comments

0
Your updated code should be as follows function convdate($date) { $formatdate = strtoupper(date('M', strtotime($date) )); $formatdate .= " " .date('d',strtotime($date)). " "; return $formatdate; } $sql = "SELECT * FROM livedates ORDER by date"; $res = mysql_query($sql); while($livedates = mysql_fetch_array($res)){ $strinoutput= convdate($livedates['date']) . "<br>"; echo $strinoutput; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.