Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,195 members, 7,822,032 topics. Date: Thursday, 09 May 2024 at 02:56 AM

My Website Date Is Not Correct Why??? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / My Website Date Is Not Correct Why??? (2540 Views)

The Date Is 29th Of November, 2016. Are You Ready For #empowertech 2.0? / The Date Is 29th Of November, 2016. Are You Ready For #empowertech 2.0? / Nairaland Upgrade Release Date Is 21st October 2016 (2) (3) (4)

(1) (Reply) (Go Down)

My Website Date Is Not Correct Why??? by IkpemhiTG(m): 11:38am On May 25, 2016
I just lunched a site and the date on my comment section happens to be different from what I had when using wamp server.What do I do? I have tried putting date_default_timezone to Africa/Lagos but no changes occur I have tried inserting this -setEnv TZ Africa/ Lagos in my .htaccess file all to no avail. so what next my fellow developer ?
Re: My Website Date Is Not Correct Why??? by ALLU: 12:34pm On May 25, 2016
Is your system time correct?
Re: My Website Date Is Not Correct Why??? by IkpemhiTG(m): 12:52pm On May 25, 2016
yaa it is ooo.
Re: My Website Date Is Not Correct Why??? by IkpemhiTG(m): 12:54pm On May 25, 2016
or could it be my php version is different from d one in my cpanel.
Re: My Website Date Is Not Correct Why??? by omoelu1(m): 8:38am On May 26, 2016
Did you add date_default_timezone before anything else on the page?
And how are you storing the data in the database? Are you using mysql's current timestamp as default or saving the time from php?
Re: My Website Date Is Not Correct Why??? by IkpemhiTG(m): 2:18pm On May 26, 2016
omoelu1:
Did you add date_default_timezone before anything else on the page?
And how are you storing the data in the database? Are you using mysql's current timestamp as default or saving the time from php?

I have added it as first line of code on d page.Yes I use timestamp in d date column on my table..I Neva had any prob wit date b4 buh dis one is giving me worries.The day month and year are correct but the time is always wrong and I can't do without it .e no go make sence
Re: My Website Date Is Not Correct Why??? by omoelu1(m): 8:55pm On May 26, 2016
IkpemhiTG:


I have added it as first line of code on d page.Yes I use timestamp in d date column on my table..I Neva had any prob wit date b4 buh dis one is giving me worries.The day month and year are correct but the time is always wrong and I can't do without it .e no go make sence

If you haven't gotten it to work, try this:
Create a new date format using php's date method
$date = new date("Y-m-d H:I:a"wink
Then save this value as your current timestamp
Re: My Website Date Is Not Correct Why??? by IkpemhiTG(m): 9:58am On May 27, 2016
omoelu1:

If you haven't gotten it to work, try this: Create a new date format using php's date method $date = new date("Y-m-d H:I:a"wink Then save this value as your current timestamp
ait...let's c how it goes...
Re: My Website Date Is Not Correct Why??? by Nobody: 11:05am On May 27, 2016
Put this inside your php script -

[php]
date_default_timezone_set('Africa/Lagos'); //this is the code to preset your time-zone
[/php]

1 Like

Re: My Website Date Is Not Correct Why??? by gmart(m): 9:09am On May 30, 2016
@Op have you been able to resolve this?

Just incase someone else gets in the same situation, here is a possible solution:

After using the php function date_default_timezone_set() to set the time_zone accordingly, if you still notice a discrepancy in your date when compared to the time_zone you've set, the culprit is most likely your data store.

If you have a CRUD application that writes to the Database using the database native function to insert datetime e.g Using php to insert a record into a MySQL database: "INSERT INTO table_name (created_at) VALUES (NOW())" (NOW() used in that insert statement is not a php function but a MySQL native function simply instructing your database to INSERT the value of its current datetime into that field. If the timezones are different, that value will differ too). Another case is when you've set a default value of 'CURRENT_TIMESTAMP' for that field, you must understand that the date that will be inserted is not affected by the timezone you've set using the date_default_timezone_set() function in your script. The inserted date will be determined based on the current time on the database server. So if my DBase server resides in a US timezone and I create records using the above methods, you'll notice these discrepancies.

Solutions:

1. Generate the date from your php script after setting the environment timezone using the date_default_timezone_set() function and then hand it to your query to insert. e.g

date_default_timezone_set('Africa/Lagos'); //set choice timezone
$current_datetime = date('Y-m-d h:i:s'); //generate current datetime for the set timezone

Your insert statement should now read: "INSERT INTO table_name (created_at) VALUES ('$current_datetime')";

The time current time for 'Africa/Lagos' timezone should now be inserted with the records on your database.

2. This second solution is not recommended, the above is a perfect but if your are not really interested in the exact datetime value. i.e you're not outputting them in the format '30 May 2016' or '30-05-2016' and only just want to display the difference between when the record was created and now. Maybe to display an 'Ago' kind of time format e.g 3 mins ago, 2 hours ago or 2 years ago. You can find the timezone that matches the current datetime of your database server and set your php environment timezone to this value, so that when you read the date from the database and run the function for the date calculation the time difference between record creation time and now still matches irrespective. This is an unrecommended hack but worth mentioning because it works.

Thank you.

(1) (Reply)

Looking For A Data Scientist To Help Me With These Questions / How Important Is CPN Certification (computer Professional Of Nigeria) / Code To Check Phone Number

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.