System Time To Server with PHP and JS - php

What I'm trying to do, is to fill in a hidden input in a login/register page for a website, and inside that input will contain the JavaScript new Date() thing, like:
"Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)"
I would like to take that information and use it server-side to convert it to the PHP date() and display it in the user's timezone.
It seems like there would be an easy way using the GMT difference of -0700, but I've looked all over and I can't find a way to simply plug that number in.
Is there an easy solution to this? All I want to do is modify the output to the users time after submitting the form.

Use date_create_from_format() or a similar function. Make sure to output the date from Javascript in the same format you intend to read.
Also see this old answer from ... myself:
How to show server and user time in PHP?

Related

Human to PHP time interpreter

We've got a collection of messy data, and trying to unify it.
Lots of services let you type dates out into different formats and they correctly understand them, but cant think what the process is called, or how we could go about doing this in PHP, if there is a library that already provides this.
So we've got time and dates in an old database we've inherited, and trying to clean it up a bit, some of the formats look like
9pm
9:00pm
25th march 2015
its a complete mix and match, does anybody know of any libraries or ways to be able to parse these into a universal format?
the problem here is the information you have is inconsistent! you need to normalize it some way, IT might actually be worth getting into an excel sheet and try to match the date time fields into some kind of regex and filter like that, is probably what i would do, so you can separate the different formats and tackle each format individually.
A program will have to first identify the format you're feeding it and then it will spit out whatever format you want!
you can use this strtotime() PHP with this to turn it into any format
http://php.net/manual/en/function.date.php

how do I acces specific external page of cgi web page using php

I'm trying to calculate the average rainfall on a tuesday in 2014 so far vs other days.
http://www.knmi.nl/klimatologie/daggegevens/index.cgi has all the information needed, the only problem is that each day can not be accessed with a direct link. An on-screen form has to be used to select a day.
Is there a way to automatically download these pages or the information on these pages with php?
EDIT: to be clear: I need all days from 2014, a day can be selected using the on-screen form.
I have no experience with cgi whatsoever.
Yes there is a way and it has nothing to do with cgi. Regardless of the programming language used to build the site; realistically you are only interested in the output of the page, which is going to tbe html in this case.
Use:
$site_html = file_get_contents('http://www.knmi.nl/klimatologie/daggegevens/index.cgi');
Now $site_html variable has the html output of the given page; you just need to parse it to get the values you want. I believe this is known as scraping web page.

how to make all users on site share a universal timezone using php or javascript?

I'm using this great jQuery timepicker http://trentrichardson.com/examples/timepicker/
It gives a string something like 07/20/2011 01:13 am
I have this situation where users on the site can schedule "events" and they are using this to pick the event. But im realizing that it could cause a lot of confusion with all the different timezones that people will be submitting from.
So I was thinking I could use php to somehow get the timezone, and the adjust how the viewer views the dates according to their timezone. But this seems like quite a task and was wondering if there was an easier way?
Also, even with the php idea I tried echo date_default_timezone_get(); as a test to see if it got my timezone, but its saying im in the Los Angelas timezone, rather than Baltimore/DC timezone.
I'm guessing its getting the timezone of my server since php is serverside.
So basically, whats the best way while using my time picker jquery script, that I can have all the users view the date/times adjusted to their timezones? Any help would be greatly appreciated.
when you get the datetime from user, save in a default timezone (GMT 0 for example), you need to know the user timezone.
When you get the datetime from DB, add the user timezone to it.
You are right, the default timezone is the server's.
get user timezone
you can save it in cookie
The term is UTC Time, Universal Coordinated Time. Googling that will find you a lot, in the meantime:
http://www.w3schools.com/jsref/jsref_getutcdate.asp
Direct give the UTC time (e.g. "Wed, 20 Jul 2011 06:10:38 GMT") to the users and use JS to convert it to their local times.
var localTime = new Date("Wed, 20 Jul 2011 06:10:38 GMT");
alert(localTime); // shows local time like: "Wed Jul 20 2011 14:10:38 GMT+0800 (CST)"

Twitter statuses timestamp help

I'm trying to get twitter statuses displaying on my blog, however I cannot get the time each status is created at to display the way in which I desire. Here is how it is being printed now:
Thu Aug 05 12:36:20 +0000 2010
However I would like it to be displayed like this:
54 days ago
How can I manage this with PHP preg_replace?
Also at the moment I am using the twitter API to get the statuses. Is it better to use this method or an RSS feed? I would appreciate if anyone could help me out. Thanks
I would really recommend writing it out to the page as "Aug 5 2010" (or however you want it to appear). That way you only need to write it out once ever, not once per day. But also write as a GMT timestamp in a way that JS can read it but people that have JS turned off cannot see it. Then, once you've got your page displaying things correctly, use a JS script to loop through the tags and replace the dates with the friendly text you want. Example:
<span class="dateToBeReplaced" title="Thu Aug 05 2010 12:36:20 GMT+0000">Aug 05 2010</span>
The JS would look something like this (uses jQuery): http://jsfiddle.net/JxTLt/4/
JS is a little more finicky about date formats than PHP, so you pretty much should stick the the format above. Use strtotime() to handle the formatting and time zone conversion.
You don't need to manually parse the date, it's already in an understandable format. If you run it through strtotime it will return a timestamp that you can work with.
The concept of displaying time as you want is called "fuzzy time", and you can find a really code post on it here.
You won't be able to do this with preg_replace() alone. You need to do some temporal comparisons to create a human readable timestamp. Check out this post. Also keep in mind that Twitter responses will be GMT.
As far as the API vs. RSS, this is really up to you. Both responses have to be parsed. There is arguably more overhead with the API now that Twitter only supports OAuth. Although there are several PHP libraries available. If you only want to display statuses, I'd go with the RSS.

want to show a time for the comment last posted through php

I want to show when the comment last posted in PHP. like 5 minutes ago, 2 days ago, 7 weeks ago. How to do this?
You can find plenty of answers with full solutions in different languages, pseudocode, ideas, etc.. here.
I believe there's an example of PHP too.
You can use timeago, a jQuery plugin, to do it via Javascript. It would yield the same result, update without refreshing, and by doing it client side instead of server side, you are not precluded from caching.
http://timeago.yarp.com/
Otherwise, Annurag has a link with some good PHP solutions.
You can do manual calculation in server, to get the time difference, then translate it into human time format.
Or my preference, do it in browser using javascript. Using this approach, the time in page can be updated without refresing the page.
You can use this jQuery EasyDate library to translate a DOM element into humane time format.
You can also read the comments in this post about Pretty Date by John Resig. It contain the code, and improvement by other.
Store the comment posted in the date DB and show the the same in the front end by comparing with current date and time using php function

Categories