Eric Haze
Eric Haze

Reputation: 133

convert mysql date and time to user friendly format?

i'm wanting to display a user friendly date format, so i am changing the mysql time stamp to keep it simpler for users. I've already got the code which echo's my desired date format and it works fine on other pages of my site. but i'm having trouble getting the date format to display in amongst a section of my code.

Here's the piece of code i'm using to convert mysql date to a user friendly format:

<?php 

    $datesent1 = $sentbox['date_sent']; 

    echo date('D M jS, Y', strtotime($datesent1));?>

And this is my code i am trying to incorporate it into:

<div class="message-date">
<?php echo "Conversation Dated:&nbsp;&nbsp;{$message['date_sent']}"; ?></div>

I should probably add that i am echoing $message['date_sent'] to retrieve the date a message was sent to the user in a table.

can anyone tell me how this is suppose to be done. Thanks, i'm new to php and mysql so sorry if it's a bit of a daft question.

Upvotes: 2

Views: 2579

Answers (3)

Davison Joseph
Davison Joseph

Reputation: 71

you can directly convert the mysql date to user friendly format by the sql query itslef, so that you can avoid the date related manipulation functions in PHP.

  SELECT CONCAT(DAY(tbl_name.date_field),' ',
  MONTHNAME(tbl_name.date_field),' ',
  YEAR(tbl_name.date_field)) as 'datelabel' FROM tbl_name

The result datelabel will a date value looks like "21 May 2014"

Upvotes: 1

RisingSun
RisingSun

Reputation: 1733

You can either save the pretty date in a variable and print it with the message or just do it where you print it like so

<?php echo "Conversation Dated:&nbsp;&nbsp;{".date('D M jS, Y', strtotime($message['date_sent']))."}"; ?>

Upvotes: 3

John Conde
John Conde

Reputation: 219934

You're so close....

...just assign the return value from date() to a variable and echo that out:

<?php 

    $datesent1 = $sentbox['date_sent']; 

    $pretty_date = date('D M jS, Y', strtotime($datesent1));?>

<div class="message-date">
<?php echo "Conversation Dated:&nbsp;&nbsp;{$pretty_date}"; ?></div>

Upvotes: 4

Related Questions