converting date/time of MySQL into GMT time/date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

converting date/time of MySQL into GMT time/date

Post by lauthiamkok »

Hi,
How can I convert the date and time from MySQL into something like this below,

last updated at 10:38 GMT, Sunday, 15 November 2009

Where I would like to have GMT - it is like ones in BBC news site,
http://news.bbc.co.uk/

this is the php code I use,

Code: Select all

<?php echo $row_page['pg_updated'];>
and the result is - 2009-11-19 02:47:02

thanks,
L
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: converting date/time of MySQL into GMT time/date

Post by iankent »

a datetime column is, as you say, formatted yyyy-mm-dd hh:mm:ss. You can use PHPs date() function to format it as you want. E.g.:

Code: Select all

echo date('jS F Y', strtotime($sqldate));
which will output something like '19th November 2009'.

Have a look at php's date manual (http://php.net/manual/en/function.date.php) for a full list of format strings

edit: updated code which was horrifically wrong lol :/ should now work

edit2: to include GMT, as you know your time is already being stored in GMT, simply output the string GMT yourself, e.g.

Code: Select all

echo date('H:i:s') . ' GMT on ' . date('jS F Y', strtotime($sqldate));
which should output something like '14:15:01 GMT on 19th November 2009'

hth
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: converting date/time of MySQL into GMT time/date

Post by lauthiamkok »

iankent wrote:a datetime column is, as you say, formatted yyyy-mm-dd hh:mm:ss. You can use PHPs date() function to format it as you want. E.g.:

Code: Select all

echo date('jS F Y', strtotime($sqldate));
which will output something like '19th November 2009'.

Have a look at php's date manual (http://php.net/manual/en/function.date.php) for a full list of format strings

edit: updated code which was horrifically wrong lol :/ should now work

edit2: to include GMT, as you know your time is already being stored in GMT, simply output the string GMT yourself, e.g.

Code: Select all

echo date('H:i:s') . ' GMT on ' . date('jS F Y', strtotime($sqldate));
which should output something like '14:15:01 GMT on 19th November 2009'

hth
Yes I think my time is being store in GMT (I guess... ) so I just include 'GMT' in the output haha - simple as that! thanks! :D
Post Reply