Could some 1 please tell how to do timestamps that not rely on users computer?
time(); and UNIX_TIMESTAMP() seems to rely on users pc

Thanks

Recommended Answers

All 19 Replies

You would need to connect to an external timeserver or use a third party app that connects to a time server. PHP only knows the time for the box it is on

You would need to connect to an external timeserver or use a third party app that connects to a time server. PHP only knows the time for the box it is on

and how should i do that?
Thanks

It depends on what you want as a source of truth for time. Using an alternate time source isn't a trivial problem. An easier approach that I would recommend, and use myself, is to keep the server/computer time synced and then just use the server/computer time inside of PHP.

Unfortunately, I don't know of any specific time servers or time applications to recommend

Dont know what u meant by "sync" but what are u suggesting is that i keep my computer time right so the fuction display the right time?

What im trying to do is a private message system But what about those who has other times on ther computers? It will get pretty ugly if the message says sent at 11:25/2005 if there computers time is messed up
Thanks for reply

Member Avatar for diafol

php and mysql work pff their servers. they dont use the user's machine time. you can set the display to the timezone of the user's choice via settings i their profile if you've given them one.

php and mysql work pff their servers. they dont use the user's machine time. you can set the display to the timezone of the user's choice via settings i their profile if you've given them one.

I have a timezone set in php.ini, but still when i change the time on my pc the time change, then how can i fix this?

There is function in PHP called date_default_timezone_set() which can set the default time zone of an application or the runtime configuration.

For example, we can define the default timezone of an application to any timezone supported.

date_default_timezone_set('Europe/London');

if we do this

echo time();

Will give us the timestamp for London. I am not sure if the time is adjusted to DST. If not, then the timestamp will be an hour late to the actual time.

If you have access to php.ini file and if you are allowed to edit it, you can add or define the date.timezone to whatever timezone you want as long as it is supported.

date.timezone = "Europe/London"

Please read this list of supported timezones.

Update:
For mysql, please refer to this documentation.

There is function in PHP called date_default_timezone_set() which can set the default time zone of an application or the runtime configuration.

I have a date.timezone set already it still change the time when i change my pc's time :/

Thanks

php and mysql work pff their servers. they dont use the user's machine time. you can set the display to the timezone of the user's choice via settings i their profile if you've given them one.

The first picture i took the screeshot minutes later that why shows a different time

2b233e439281846f62525ab527930f68187e011cf7747be9c8942845f6f9d0c3

can you try running this

<?php

     phpinfo();

direct your browser to this page and look for the loaded configuration file. It should tell you which php.ini is being loaded by the xampp.

can you try running this

time zone is correct see below
870cd530c7eb7e9786f67345b7909a46

Member Avatar for diafol

You are running this on a local server right? So if you change the local machine system time, you will change the server time. When running on a remote system, you will be on 'their' server time, which will not be under your or anybody else's control, other than your host. This is a separate issue to the uses of timezones.

Ah, a chat system. If you are using a single server(the most likely scenario), then you use that server's time.

You then make a client that each user would use to connect to the server. (a browser page for example) Any message must first be sent to the server, and then the server send it to the client (and a confirmation back to the original sender for their timestamp. The clients can then set the local time zone however you want to let them.

Another option is to not use a server and let each client set the time stamp for when it displays the message. The only potential drawback, is that two separate clients might have different timestamps for the same conversation. This is the easier approach.

One thing to note for a client application - unless run through browser requests, PHP is not a wonderful for installing on client boxes.

Added note: I think this was described, but you could also request the current time from a SQL server.

Member Avatar for diafol

A PMS will store messages in a DB, usually with a timestamp (unix flavoured) or sometimes with a datetime. Timestamp is favoured as this makes it easier to provide users with displayed datetimes conforming to their timezones - but only if you give them the ability to set their timezones, e.g. in a users table.

e.g.

user_id
username
passhash
...
datetime_format ("d/m/Y h:i:s a")
timezone ("Europe/London")

This can then be used when perusing the site, e.g. the PMS...

date_default_timezone_set($userTZ);
//...
echo date($userDtFormat,$msg['timestamp']);

In the end, there are a lot of suggested approaches. A db is a heavy handed solution, imo, but it would also work. Regardless, I think the original question has been answered quite thoroughly.

Member Avatar for diafol

A db is a heavy handed solution, imo

How would you suggest storing private messages otherwise?

You are running this on a local server right? So if you change the local machine system time, you will change the server time. When running on a remote system, you will be on 'their' server time, which will not be under your or anybody else's control, other than your host. This is a separate issue to the uses of timezones

I figuered this out yesturday cuz i tried it in ipage and it worked fine :3 so my guess was what u say, Many many thanks!

@centenond I am having a same issue. Please let me know how did you figured out this problem

Member Avatar for diafol

You set the timezone of your choice in your pages - for display purposes:

date_default_timezone_set($userTZ);

Be careful if storing datetime format - ensure that you use YOUR choice of TZ, not user. Storing - it's safer to use timestamp (integer).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.