How to get the computer time in php/laravel/js - localtime

I would like to see a solution for this that dont use timezone changing in php or JS, just a function that gets time computer time from windows directly. Windows has already a service that sincronize the computer to local time, so if you cen get this time should be very nice.

I got this in JS. It gets the computer time!! Not the server's.
<script type="text/javascript">
var data = new Date();
document.write(data.getHours() + ':' + data.getMinutes());
</script>

Related

Laravel store the time difference into server

Ok, so now I'm building a VueJS + Laravel web application. My main function for the app is when it reaches a certain time(startTime), the user will be able to click a button and store the time difference between the time when user pressed the button and the startTime to the database. Note: there will be around 25users submitting at the same time, spamming the record button. Each user can only record once. The one with the shortest difference in time will win the competition.
Currently, I've thought of two ways,
(i) Use javascript Date.now() to get the current time and subtract by the start time and then send the timeDiff from front end to backend. It's fast but Date.now() depends on the client system time, which if the user changes its' system time, they can either draw earlier or later since it doesn't matter as long as the time matches the start time.
(ii) Everything is processed in the backend. A timestamp will be generated on the server every time a user press the record button. It doesn't have the issue (i) has but due to the performance of Laravel, it looks like the server is stalling the request thus making the timestamp not accurate at all.
Any suggestions or advices? I'm still new to all this and these are the 2 methods I can think of right now.
if(auth()->user()->recorded !== 1){ //check if recorded
//create timestamp in epoch
$pressTime = microtime(true)*1000;
$offTimeStart = $user->startTime;
$offTimeEnd = $user->endTime;
$pressedTime = $user->pressTime;//initially 999999999
//prevent user spamming record btn
if($pressTime > $pressedTime){ return something;}
//check if still within the range
if($pressTime >= $offTimeStart && $pressTime < $offTimeEnd){
if($user != null){
$user->update([
'pressTime' => $pressTime,
'recorded' => 1
]);}

Timezone and time not synchronized on client pc

I have created an application that depends on the timezone (set on the OS) of the client to render a display.
However, if the client incorrectly set his timezone on his pc, my display breaks.
My question is: How to i get the appropriate timezone of the client, based on where he is right now?
I understood that there are a couple of solutions: I could do this by using google API and then know the location of the client.However, this would require asking the client permission before accessing his location. I also read that you could use IP address of the client but this is unreliable. Finally, I could just ask the client.
Is there any other way to do this dynamically?
Any help is appreciated
PS: I understand that the question is broad and my question would simply require a list of possibilities. No definite answer.
Javascript new Date().toString(); would give you all the information you need about the time at the user and the timezone (if set properly). You can then send it to your server and do what you intend to.
var split = new Date().toString().split(" ");
var timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1]; //outputs "GMT+1100 (AEDT)"
No permission needed for this one. Just sends it back to your server as you want and work with it.
Javascript code from here: Getting the client's timezone in JavaScript

Extremely slow WordPress user import on XAMPP

I'm posting this question here because I'm not sure it's a WordPress issue.
I'm running XAMPP on my local system, with 512MB max headroom and a 2.5-hour php timeout. I'm importing about 11,000 records into the WordPress wp_user and wp_usermeta tables via a custom script. The only unknown quantity (performance-wise) on the WordPress end is the wp_insert_user and update_user_meta calls. Otherwise it's a straight CSV import.
The process to import 11,000 users and create 180,000 usermeta entries took over 2 hours to complete. It was importing about 120 records a minute. That seems awfully slow.
Are there known performance issues importing user data into WordPress? A quick Google search was unproductive (for me).
Are there settings I should be tweaking beyond the timeout in XAMPP? Is its mySQL implementation notoriously slow?
I've read something about virus software dramatically slowing down XAMPP. Is this a myth?
yes, there are few issues with local vs. hosted. One of the important things to remember is the max_execution time for php script. You may need to reset the timer once a while during the data upload.
I suppose you have some loop which takes the data row by row from CSV file for example and uses SQL query to insert it into WP database. I usually put this simple snippet into my loop so it will keep the PHP max_exec_time reset:
$counter = 1;
// some upload query
if (($handle = fopen("some-file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
mysql_query..... blablabla....
// snippet
if($counter == '20') // this count 20 loops and resets the counter
{
set_time_limit(0);
$counter = 0;
}
$counter = $counter + 1;
} //end of the loop
.. also BTW 512MB room is not much if the database is big. Count how much resources is taking your OS and all running apps. I have ove 2Gb WO database and my MySql needs a lot of RAM to run fast. (depends on the query you are using as well)

Should I rely on the local phone's time for time sensitive app (Windows Phone 7)

I'm building an app where my users will post content. The exact time of the post is an important data point - I need to know exactly when the user hit the "Post" button. Once the post has been captured I'll upload that posting to my web server. My app should still work in offline mode, meaning when there is no internet connectivity the post will be saved locally and uploaded next time the network becomes available.
Question is, how can I guarantee that the time of the post is accurate? Should I rely on the phone's local time? Should I try to create some crazy code that regularly sync's the difference between my servers time and the devices time so I can always know the difference (if there is one). Are there better time management solutions that I'm not aware of?
Thanks,
UPDATE
Here's the server side code that I wrote to ensure that server and client times are perfectly matched. Hope it helps others...
/// <summary>
/// Calculates the actual time the client event occurred.
/// Takes in account that the event and the sending of the
/// event may have happened seprately.
/// </summary>
public static DateTime CalculateClientEventTime(
DateTime serverReceiveTime,
DateTime clientSendTime,
DateTime clientEventTime)
{
// first we need to sync the client and server time
// we also need to subtract any time zone offsets
// then we can subtract the actual time on de ice
DateTime serverReceiveUtc = serverReceiveTime.ToUniversalTime();
DateTime clientSendUtc = clientSendTime.ToUniversalTime();
DateTime clientEventUtc = clientEventTime.ToUniversalTime();
// note: all dates are in utc
// just need to subtract the client TimeSpan from the client Send
// then subtract that TimeSpan from the server utc time
TimeSpan diffBetweenClientEventAndClientSend = (clientSendUtc - clientEventUtc);
return serverReceiveUtc.Subtract(diffBetweenClientEventAndClientSend);
}
I suggest that you do the following:
In online mode: Take the time from your server when the user post their data.
In offline mode: Save the time from the phone. When going online, submit all saved data, and the current time of the phone. Calculate the difference between the phone and your server time to get the real time.
You cannot rely on the phone's time because user can change it and your app can run in diffrent time zones. Use always the sever time or you can get the phone time and calibrate your local timer to get the value of a lag.

Any body have any luck with ShellTileSchedule?

Any body have any luck with ShellTileSchedule? I have followed the Microsoft example and still have gotten no where.
"How to: Update Your Tile Without Push Notifications for Windows Phone"
Has any one seen a complete example that works on a device or emulator?
Yes...I started with the sample at http://channel9.msdn.com/learn/courses/WP7TrainingKit/WP7Silverlight/UsingPushNotificationsLab/Exercise-2-Introduction-to-the-Toast-and-Tile-Notifications-for-Alerts/
and skipped immediately down to "Task 3 – Processing Scheduled Tile Notifications on the Phone." After that I had to wait about 1 hour, leaving the emulator running on my desktop (1 hour is the minimum update interval, indicated as such for "performance considerations."
_shellTileSchedule = new ShellTileSchedule
{
Recurrence = UpdateRecurrence.Interval,
Interval = UpdateInterval.EveryHour,
StartTime = DateTime.Now - TimeSpan.FromMinutes(59),
RemoteImageUri = new Uri(#"http://cdn3.afterdawn.fi/news/small/windows-phone-7-series.png")
};
Note that setting the StartTime to DateTime.Now - 59 minutes did nothing. It still waited a full hour for its first update. I could not find any mechanism to perform "go to this URI and Update yourself NOW!", other than calling out to a web service that tickles a Tile Notification.
as #avidgator said, you'll have to wait an hour.
i have written a tutorial on how to update the tile instantly here:
http://www.diaryofaninja.com/blog/2011/04/03/windows-phone-7-live-tile-schedules-ndash-executing-instant-live-tile-updates
basically it involves opening a push/toast update channel and then getting the phone to send "itself" a live tile update request. this will trigger the phone to go and get the tile "right now"
hope this helps
Are the channels necessary for this kind of update?
Is there a full code example of what has to be done to create an app that just updates its tile?
BTW: How about setting the Recurrence to UpdateRecurrence.Onetime and the StartTime to Now + 20 seconds for testing purposes?
I just got an tile update after an hour without channels and so on. So that answered my first question. But having to wait an hour while trying to develop an app is... unsatisfying.
It is easy. Just use the following code when you setup ShellTileSchedule.
ShellTile applicationTile = ShellTile.ActiveTiles.First();
applicationTile.Update(
new StandardTileData {
BackgroundImage = new Uri("www.ash.com/logo.jpg"),
Title = ""
});

Resources