Modify date() to give gmt

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
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Modify date() to give gmt

Post by Shendemiar »

<?php
echo (date("G:i , D d M Y"));
?>

This gives the time of the computer runned in.

Is there a easy way to modify the hours date results, something like date("G+5:i , D d M Y")
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

echo date("G:i , D d M Y",strtotime("now +5 hours"));
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

:? :) :o :D :lol:

Thanks!
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Gives nice gmt time.

Code: Select all

echo date("G:i",strtotime("now +6 hours"))." GMT, ".date("D d M Y");
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Now the dates go wrong...

Anyone know easy script to receive GMT or to enhance date() so that the date part also realizes the timedifference?
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

I'm using this currently, seems to work, but i havent verified the date-thing yet.

echo 'GMT: '.gmdate("G:i, D d M Y",time() + (3600 * $timezone));
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Using something like gmdate("jS F Y") will give you the GMT date (like you've done above).

You only need to add any additional code if you want to get time-offsets from GMT... for example gmdate("jS F Y", strtotime("+1 hour", time())) would give you the time in Paris (or GMT + 1)
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

I had that kind earlier, but it didnt work: adding hours to time just resulted strange hours if the server was on different day than the viewer.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

That's strange, it's always worked for me. By default time() will/should give you a unix-timestamp (time of seconds since January 1 1970 00:00:00 GMT) which by default is GMT as you can see.

Might be worth checking out [php_man]date[/php_man] [php_man]time[/php_man] and [php_man]mktime[/php_man] in the manual.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

You're correct. i didnt had exactly that you suggested but

Code: Select all

echo date("G:i , D d M Y",strtotime("now +5 hours"));
instead.



But now i'm allready much more wiser
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Glad to be of help :)

I remember that the whole PHP date thing confused me a bit when I first started using PHP... it's not too bad once you get used to all the different date/time formats though.

Good luck.
Post Reply