0

Here's my PHP code:

echo date('M j y g:i:A');

This doesn't display correct time. why?

13
  • Check the server's time zone. Commented Mar 8, 2011 at 19:44
  • i mean it displays correct date. but incorrect time. Commented Mar 8, 2011 at 19:48
  • @rocket: you again?haha. how to check server's time? Commented Mar 8, 2011 at 19:48
  • 3
    What time does it display? Geez. What is the current time, and what time do you get? Commented Mar 8, 2011 at 19:48
  • @pekka: sorry, it displays march 8 2011, 7:49pm. but right now here in phil its 3:50am already. Commented Mar 8, 2011 at 19:50

3 Answers 3

3

Insert this at the very top of your code:

date_default_timezone_set('Asia/Manila');
Sign up to request clarification or add additional context in comments.

7 Comments

follow up question. how to insert date and time to a database with datetime datatype, so i can query it and display the inserted date and time.?
Just use NOW(), like: INSERT INTO table VALUES ("random value", NOW())
i used it, but i came back to same problem with incorrect time. ive inserted the default time zone. but it wont work on queried dates. help?
by the way heres how i echo my date from database- echo date('M j y g:i:A', strtotime($fetchcom['date']));
Right after you connected to the database, do this query: SET time_zone = 'Asia/Manila' and then try to insert time again.
|
1

fix the time zone in php.ini or in the code itself

Add something like this to your php.ini file date.timezone = "Asia/Manila", that will set the default without having you put in the date_default_timezone_set('Asia/Manila'); on every file that uses time functions

5 Comments

Add something like this to your php.ini file date.timezone = "Asia/Manila", that will set the default without having you put in the date_default_timezone_set('Asia/Manila'); on every file that uses time functions.
@New Guy im adding that to the answer
where to locate php.ini file?
usually if in UNIX/LAMP than in the etc folder
so look for the php.ini in the PHP folder
0

It's most likely echoing the time on the server, which is often GMT. If you're time zone is GMT+5, you may need to add 5 hours to the timestamp.

For 5 hours...

$offset = 5 * 60 * 60;
echo date('M j y g:i:A', time() + $offset);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.