I have a table of 'notifications' which may or may not be sent in any 24 hour period. Each notification has it's own timezone offset from GMT, so I have a column for offset which will be +/- in seconds.
I need to create a PHP script which will regularly (via cron) check the database for sent notifications, and then reset them to unsent if their time has passed 00:00 for that day. I'm really struggling with the best way to do this - is there a simple calculation I can make?
I was thinking of something around getting the UNIX time for the server's midnight using mktime() [my server is in GMT], and working it out with the offset in seconds, but every way I think of seems wrong.
Something like:
if(!mktime()-$notifcationTimezone > $serverMidnight){}
I'm not saying this doesn't work, but I don't think it will work when the server elapses into another day. It just confuses me, and I have no faith in it.
Isnt this just a simple comparison with strtotime and you dont even need to add up the offset. You can use the date function to set a specific time you want to pass to the strtotime which you want to use to compaire against.
To give us a better idea show us a real example from you database and what the cron looks like right now.
I'm not totaly sure what you are trying to pull of here but, what you might wanna think about is do you really want to reset anything, is it needed? You could fill in at what date your notification got send. If it does not match the date of today you want to resend it again?
Related
I need to debug all kinds of time-related issues. One bug only happens for one hour every six months. It's tedious to wait half a year for a one-hour window to fix this bug. Due to this, and similar test situations, I would like to "fake the system clock/date" for an individual PHP script.
It must not change the actual system clock/date! Actually changing that would be catastrophic for all kinds of logs I keep, no matter how briefly.
I want to be able to do something like:
set_fake_time_for_this_script('1995-05-04 04:24:15');
sleep(3);
var_dump(time()); // Will not return the timestamp for my computer's system clock, but instead "1995-05-04 04:24:18".
// Any other time-related functions will also use that fake time/date as the "present time".
Is this possible? If so, how? Please tell me it is...
This is not possible the way PHP implements the time functionality.
PHP reads the time from the server it's hosted on(in your case, can be your pc) and therefore you cannot change the time just like that.
There however is a clever method of doing what you need. Since time is just a timestamp at its core, and that's a number, find the difference (positive or negative) of timestamp between the current time and the time you want to set in your function. You will get an offset.
Then perform all computation that you want to do with time, and add the offset to that. This way you will get all functionality of time without implementing the whole time functions again.
function mytime() {
return time()+set_mytime();
}
function set_mytime($new_time=null) {
static $offset;
if(!$new_time) {return $offset;}
//$new_time should be a unix timestamp as generated by time(), mktime() or strtotime(), etc
$real_time=time();
$offset=$real_time-$new_time;
}
Also, if you are very much interested in changing the time, you can use the exec function and call the system commands to do that. However please note that changing time for entire system randomly will have unexpected results.
According to Yahoo, my account is using MySQL 4.1.14, and PHP 4. Yeah, I know I am a bit behind. I noticed some funny things started going on with my code a while back, around dates. But I thought time zone setting did not come into play until PHP 5.
Anyway, I am pulling dates from a database using UNIX_TIMESTAMP, and it gives me a timestamp like 1436745600 for a date 2015-07-13 stored in my database. This looks correct to me. But then my code simply does a date('M-j','variable storing that number'), and it gives me 'July-12'. Why is this?
I know I am PST, and the server is UTC, but on version 4, how would PHP know that? And even if it did, we are both on the same day for most of a 24 hour cycle. As I started writing this, we were on the same day, and still the problem occurred. As I finish, we are on different days, and the result is the same. Something else going on here?
How does PHP time() work in following case:
User is posting something in database and posting stamp is added also which is just time().
But how to compensate for time zones? What if two users from EU and USA are posting at the same time, will the posting stamp be the same?
The whole point is that the time()-method stores everything in the same timezone, the one the server is configured with.
If it did not do this, it would be impossible to know when something really happened.
If you want to display datetime with timezones, you can output the timestamp from the server, use javascript or something to calculate the clients timezone offset and add/substract hours to the time.
I have setup a web app for a client. The server is in US I believe. The client himself is in Sydney.
He says he added a record to a table with a timestamp field and it shows the timestamp as 2010-11-19 07:12:00 but the actual local time has been 2010-11-20 13:12:00.
So he is 30 hours ahead of the time recorded. This doesn't look right, how could the offset be greater than 24 hours? And what should I change/use so he sees correct date/time?
Edit:
Would it be correct approach to add 30 hours to current time before inserting the record?
First thing is to check the time on the server. Make sure that is correct.
To fix the issue, you have to know the offset for the user, so this will either need to be a recorded in a user profile, or if it is stationary for the whole application, you can set the server's time to correspond with his timezone.
Another option is using PHP's DateTime Class.
Another option would be to use certain Timezone Setting functions PHP offers.
A final option would be to set the timezone setting for MySQL this requires you to add items to the startup options, so you will need to edit the startup script for MySQL.
If the offset is 30 hours, then the culprit can't possibly be the time zone. Or at least, it can't be the only culprit. Without more details, that's the best I can do.
Sydney, Australia is GMT+10. The east coast of the US is GMT-5. That's a 15 hour difference.
Could be a coding error, where the timezone is getting corrected twice.
I have a social network site I have been working on for a couple years in PHP/MySQL, I am now re-buidling the whole site from scratch again though. This time around I would really like to add in the ability for users to set a timezone for times on my site.
So with that said, I am asking on a guide from start to finish of the steps I need to include to do this please. My old site used mysql datetime for all dates and times and it worked great but I read that it is best to use like a text filed and store all dates and times with UTC, can someone explain how I could do this? Would I still be able to use the now() function in php to save a time to the mysql?
Also I have seen the list that php can generate of all the timezones, the one where it shows like a million (not really) but I am wondering, would it be possible to show some sort of map with images or something and link to the main timezones?
Please any tips for setting a users timezone, I can do that part, but once I have a user's timezone saved and ready to use, how can I make sure the users see's the correct time and how do I save times in the correct time.
Sorry if this was confusing, any help would be great though, thanks.
When working with php, from my experience, it is the best to store timestamps, generated with time(), as an int in the database. While it may not be that easy to read them when looking at the database (as you cannot guess the actual date just from the number), but the timestamp is the most-native thing when working with dates in php.
To save each user's timezone, you can simply save the offset, in either hours, or better (as some timezones are half an hour off) in minutes. So for example my timezone offset would be +60 (UTC+1), while in America most will have something like -480 (UTC-8) or similar.
Then when displaying the times for a user, you just pick the timestamp (which is in UTC time) and the timezone offset and generate a readable date from it using the standard date() function for formatting.
For example:
<?php
$time = time(); // from database or time() for "now"
$offset = -480; // from database
// add the offset to the time you want to display and you have the user's time
echo date( 'd.m.Y H:i', $time + $offset * 60 );
?>
In addition you could then also store the formatting string (d.m.Y H:i) in the database, so that every user can pick his favorite format.
This topic gets complicated especially when you factor in daylight savings and the fact that your server will be in a different time zone from some of your members.
I suggest you take a look at some libraries out there that have been built to handle these issues. The PEAR package Date might be a good place to start. http://pear.php.net/package/Date
Basically, you would want your members to select a timezone for their profile. Then convert all times to UTC (to remove the offset of the server location) and store them in your database as a timestamp. When displaying the times, apply the timezone offset the member chose for themself (maybe with UTC as the default) as well as a date display format.