Date() not showing the right time


#1

Hey guys!

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..

<?php


date_default_timezone_set('Australia/Melbourne'); 

$dt2=date("Y-m-d H:i:s");
echo $dt2;

?>

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


#2

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.


#3

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..


#4

Correct.

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.


#5

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


#6

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


#7

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


How to insert datetime in sql query
#8

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


#9

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...


#10

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.


#11

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


#12

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?


#13

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

http://php.net/manual/en/function.date.php


#14

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?


#15

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?


#16

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?


#17

According to the documentation ( https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html ) 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?


#18

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...


#19

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


#20

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?