I have a php project wherein there is option to select date for sending notifications on predefined interval starting from the selected date.
for eg. send notification to users on 5th of every month starting today.
My php script checks the system's/server's current date and time and run the scheduler accordingly to send the notifications.
I can test this by changing system's date and time but my question is i want to test whether the notifications are out on 5th date of every month without changing the system date and time.
I can't change the system's date and time due to some reasons. So can we simulate the future dates and let php identify that date as current date. If this is possible, please let me know how to achieve it.
Edit : one more thing i would like to add. I want to test this thing as a tester so i can't change the existing code.
Thanks & Regards
Zeeshan Shaikh
YOu can try http://php.net/manual/en/function.override-function.php to override time() function (or what you use to get the date and time).
Related
We have the same system as Google Calendar Events now you can create an event and it will notify you at that time.
PHP cron job will run every 15 minutes, now the settings can be set to send mails every day or weekday or weekend, or every 2nd day of month or weekend. And it can be repeated for few occurrences or forever so we can not store UTC value in the table.I know I can store the UTC for next event and update that column for next event. But I think, there should be a better way to do this then changing UTC on each occurance.
we have a field which was storing offset time "-04:00" or "-05:00" on the basis of timezone you choose.
We had EST, EDT, CST, CDT, PST, PDT, MST, MDT in our drop-down earlier. on the basis of chosen value, we have saved offset in DB which is an incorrect way.
Now when DAYLIGHT SAVING time changes user has to move from EST to EDT or vice versa. Now we want to solve this problem that user does not have to change there settings.
Want to have a drop-down which has "Eastern, Central, Mountain, Pacific" and want to store the value in DB.
What value should be stored ? as we do not want to store offset as it will change with DST and it will be an incorrect way.
Another thing how it works. Our servers are in UTC. we have a custom function in MySQL named "isItCorrectTimeToDoThis" which checks that is there any events are present at this time in UTC in time zones. in this function, we are using the CONVERT_TZ function. We have cron job which runs every 15 minutes but a query to get events is in MySql. I hope you guys have understood this part.
We have Timezone tables installed in our server.
WHat should be stored in db table field timezone "EST", "EDT" or "US/Eastern".
1> I am not able to find what will be the output of CONVERT_TZ(now(),'UTC','EST'); when DST is on.
OR
2> I am not able to find what will be the output of CONVERT_TZ(now(),'UTC','EDT'); when DST is off.
Or
3> CONVERT_TZ(now(),'UTC','US/Eastern')
Which one will handle Daylight savings accurately? That user does not have to change their settings again and again.
Just store the time in UTC and just render the time for the local timezone of the user. This is the best option. The only conversion will be in the UI and that is the only place where you should care for time zones.
Not sure if it's even possible but is there a way to auto update a database field on a certain date?
I've just been given a system to work on and there is zero documentation on the processes and no comments within the code and more than 2000 files with thousands of lines of poorly written code. Btw it's a custom MVC system.
Now the current system automatically updates a database field 24 hours in advance before the date arrives. There are no cronjobs running. Could it be possible to auto update a field when a certain date occurs? Or is the code being triggered somehow? Can this be done via a PHP script?
Basically the system expires items 24 hours before their expiry date arrives.
Already been through the following material but no help so far:
PHP and MYSQL date expiry near
get a time difference between two dates
PHP MySQL - Select all where expiry date = todays date + 7 days
http://www.interactivetools.com/forum/forum-posts.php?postNum=2215219
http://www.dreamincode.net/forums/topic/317765-how-to-set-expiration-date-in-php/
I think you can draw attention at one more feature MySql - "Using the Event Scheduler"
https://dev.mysql.com/doc/refman/5.1/en/event-scheduler.html
I ran into trouble while dealing with date and time using php and mysql. I am trying to store local time of user's timezone as a timestamp on mysql db and would like it to convert back to normal date and time at the time of output.
This is the first time I am dealing with date and time.
As I understand: I can't rely on PHP's time() as it returns servers time according to server's timezone and the same case with mysql current_timestamp.
I can use the javascript to get user's local timezone and then can use date_default_timezone_set() for each session.
If I am doing right, now the confusion starts.
As you can understand the users will come from around the globe, so if two users (one from US and another from India) do something at the same time, will it show each others time as identical or it will show some difference? I mean it shouldn't show Indian user that the US user has done something few hours ago as the US user done at same time.
Please let me know if I don't understand these things properly.
What I want to achieve is, the output of the time should show in local time format. Ex: any time should show in IST format for Indian user and other country respectively.
Best way to achieve this is to store date in a database in a standard format (eg. GMT).
After getting user's timezone through javascript, you may convert date to user's timezone & display accordingly.
Well I know that this question may be asked many times but certainly I've few doubts In my mind, and by the way don't comment that what I've tried, I've tried many ways but am just asking what's the correct and easier way as there are many posts lingering out here with different suggestions for accomplishing these tasks, so I'll explode() my question into smaller questions...
So can you people just guide me where am going right or wrong as am sure many people are confused when it comes to date/time
1) Why/how to save time as UTC in MySQL using PHP?
Personally for this I post this using php $year/$month/$day and check the date using checkdate() in mysql date field. so is it ok or should I use timestamp and than explode the retrieved string on front end using php?
2) If I run server from India, should I record default time using date_default_timezone_set("Asia/Calcutta"); and than subtract and add time using php function or while posting only I should check users time zone selected from his user accound and accordingly set a condition kinda:
if(timezoneselected == +5.30) {
echo date_default_timezone_set("Asia/Calcutta");
} elseif(timezoneselected == +anytime) {
echo another country timezone
}
3) last question is how websites like gmail facebook etc manages time? I mean if they are saving datetime according to their server than how they show perfect posted time for each user, even gmail, if I send a mail to another user, my sent time and the person living in another country gets email at his printed local time I mean how we can do this, sorry am not able to explain you perfectly say this example,
facebook:
user from India posts, facebook shows posted 8 mins ago, 9mins ago, fine after sometime they show a real date, and that date is perfect according to the time I posted, however if a person from USA updates, say 8 mins ago 9mins ago on his profile but his original posted time is shown correctly to him, and even correctly to me?
sorry for this question but really this will help me understanding this date/time concept and will also be helpful to future users. Thank you!
Bottom line, you should store everything UTC
When you display times for a particular user, use a timezone of their choosing. Store the timezone of the user, like "Asia/Calcutta" and simply convert the time when displaying it using the date_default_timezone_set method.
I will attempt to answer your questions from the comment here.
You store everything UTC always. It is the baseline. When you display the times associated with anything you convert based on the user. If you want to display Posted 8 mins ago then you are taking the delta between the current UTC time and the UTC time associated with a post. If you send a message from user A (in India) to user B (in Los Angeles, USA) then you would store the message time in UTC. If user A is viewing it, the time would be converted to "Asia/Calcutta" and if user B is viewing it, the time would be converted to "America/Los_Angeles". Using UTC will make your life a lot easier. Trust me.
As described in MySQL Server Time Zone Support:
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
Therefore, if you use TIMESTAMP type columns, MySQL will handle timezone conversion for you automatically: just set the appropriate timezone for the session in its time_zone variable.
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?