Date() not showing the right time

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.

1 Like

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…


It won’t, that’s been mentioned several times.

You can manipulate dates and times inside PHP, but just not in the way you are doing it.

1 Like

Use DateTime within PHP, different syntax, but as easy to use.

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.

1 or even better, DateTimeImmutable


Thanks for the help guys and i think i will just stick with the sql now ()… i think it is easier

DateTime has a lot of syntax to give, so if you speak to it very well, you get what you want:

var_dump((new DateTime('2018-01-31'))->modify('last day of next month')); // 2018-02-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…

Dates also differ per timezone.

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.

1 Like

Thanks for the assistance and I will read through more careful next time

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?

If you don’t specify a timestamp as an optional second parameter to the date() function, it will use the current date and time.

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, you can see it changes, but it doesn’t change? Not sure what you mean. Do you mean that when you edit the column it doesn’t store the result?

1 Like

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?

According to the documentation ( ) if you set the column to auto-update, then whenever you update that row it should update the timestamp.

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?

1 Like

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…

And how does auto update fit in with that? Sounds like regular updates to me.

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?

I’m still confused as to why you want to do that. You just store when the subscription starts and expires then you can check in the code when a subscription has ended. No updates are required here at all.

In pseudo code:

if (now() > expiry_date) {

1 Like