简体   繁体   中英

Displaying Date from MySQL Database

I know this is covered a lot in SO, but i can't find the exact solution i need so please bear with me. When users submit an entry to my database, i'm storing the date and time in a DATETIME field called date.

Now when i come to echo the date their particular entry was posted, i'm using:

<p>Posted:<br /><?php echo $rsjobinfo['date'];?></p>

Of course this is displaying the full date and time and i only want to display something like 7 July, 2011.

So above my echo statement i've done this:

$date = $rsjobinfo['date'];
$date = date('j F, Y');

Which, if i echo $date, displays today's date in the correct format, but i cannot figure out how to apply that format to the date that's being pulled from the database. If i replace

  <p>Posted:<br /><?php echo $rsjobinfo['date'];?></p>

with

  <p>Posted:<br /><?php echo $date;?></p>

It just echo's todays date (albeit in the format i want), rather than the database date.

Can someone help?

Thanks in advance, Dan

You need to pass the right date to date() . See http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.strtotime.php

$date = date('j F, Y', strtotime($rsjobinfo['date']));
$date = date('j F, Y', strtotime($rsjobinfo['date']));

See the following documentation for more details:

As other suggested, you should use PHP functions like strtotime() and date() :

$date = $rsjobinfo['date'] ;
$dateToPrint = date('j F, Y', strtotime($date) ) ;
echo $dateToPrint ;

You could also use MySQL DATE_FORMAT() function:

$sql = "SELECT ...
             , `date`
             , DATE_FORMAT(`date`, '%e %M, %Y') AS dateToPrint 
        FROM 
            ...
       " ;

$dateToPrint = $rsjobinfo['dateToPrint'] ;
echo $dateToPrint ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM