I am trying to learn about the date() and now() function and I know that someone mentioned that the now() is not php. I posted a question about this in the other post but based on the date(), it doesn’t return the correct date unless I do this…
But then I am confused because if a user from another country were to sign up, would it display a incorrect date? This is weird because when I checked for the timezone: it says
Europe/Berlin
but after checking the computer setting, it says that my time is checked for Australia’s timezone
Php doesn’t use the computer timezone, you need to provide one to it to use, as you’ve found out.
And yes, that also means that when people from a different timezone use your site they will see a “wrong” time. You would need to request the timezone they use (which you can do using Javascript I believe, or use GeoIP to determine where they are and which timezone that is) and then store everything in UTC and convert to the user timezone.
So I guess my easier option is to use now () but i think this is only a sql syntax and not a php one? So i can’t assign $row [‘date’]=now () + interval ‘1’ month;…i tried it but it didn’t work…
No, because now() also returns the time for the timezone MySQL is configured in. Different software, same problem.
No, as @chorn suggests you should use the DateTime1 class for that
$row['date'] = (new DateTime('+1 month'))->format('Y-m-d H:i:s');
Also you need to really careful with adding and subsctracting months, as for example according to PHP January 31st + 1 month = March 3rd, since February only has 28 days and the remaining 3 days “roll over” into March.
It’s best to not use months but just always use something like 30 days instead, much easier to explain to non-technical people as well, who would probably expect that January 31st + 1 month = February 28.
So if I were to use date_default_timezone_set(), must I select those countries that I want? I guess that i would need to use one for Europe, USA, Britain and Australia, just to name a few if I am concerned about the time… I guess if the time is not that important, I can just stick with the dates…
You should not use the default timezone in PHP, but the determine per request which timezone is needed. Which you could do by converting the IP of the visitor to a geolocation (there are tools for that) and checking in which timezone that location falls, and then use that timezone for that request.
As I’ve already stated before by the way. Please stop scanning answers and start reading them.
I can get my date to work by adding this code and setting my database to current timestamp and setting on updating current timestamp but I thought that this would update the time and date whenever I refresh my database? It doesn’t seem to be updated though…
$subscriptionplandate =d date(‘Y-m-d H:m:s’);
I am assuming that instead of waiting for you tomorrow, I should be able to change my date setting to tomorrow and therefore the database should be updated?
In my database table, I have two columns, one is to display the date of when the user subscribed for a lesson and the other one is the expiry date. I have set timestamp for both but only set auto update fir the former one… I can only see the time changes when i am editing that column but it doesn’t seem to be changing. …
I read that I have to update ther user’ timestamp by quering…is this the only way?
Sorry for my unclear explanation but i read somewhere that the timestamp is only meant to change if I update the table, is this true and does that mean that I must always update the user’s timestamp and compare against their expiry date everytime they were to access their subscribed plan?
I can’t see, though, why you would want either the start-date or the expiry-date set to auto-update. Surely once set, these would stay the same until you or the user extends or cancels the subscription?
For a couple of services, the user has to pay for a monthly access and that is why i want to have a date to show when they buy the course and to cancel that in a month’s time…
From what i read, if i set to auto update, this will only be updated when the row is being edited. I have checked the date of the timestamp today and it is still show yesterday’s time. Is there a way to update the time on a daily basis?