[thelist] javascript date into php

Bob Meetin bobm at dottedi.biz
Mon Jun 24 09:02:11 CDT 2013


With the pointers in hand, I got this working, googling for an out-of-the-box solution and ended up with:

http://jtest.us/code/get_local_timezone.php
http://jtest.us/code/get_local_timezone.txt

Today in History is now happy.

-Thx, Bob

  On 06/21/2013 08:49 PM, Renoir B. wrote:
>
> I was about to write somwthing similar to Simeon. But he already did.
>
> If you want the currend date, on your server, you can use \DateTime();
>
> But if you want the visitor's current date into your own. Its a whole different matter.
>
> Just have a PHP file accepting POST to it and read what you post to it using var_dump
>
> To send to it, use similar JavaScript. I wrote this quickly on my phone, but it should work.
>
> In the visitor page. Put this javascript, it will initiate an AJAX request when possible.
>
> <script>
> $(document).ready(function(){
> $.ajax({
>   url: '/hello.php',
>   method: 'POST',
>   async: false,
>   data: {date: (new Date()).toString()}
> }):
> });
> </script>
>
> In file.php
>
> <?php
> var_dump($_POST);
>
> This use cas is useful to know the visitor current time compared to your local time only. This is one of the thing that google analytics is doing along with geographical, and browser data.
>
> Please note that JavaScript has this /feature/ to add automatically semi columns at the end of every line. But it is most strongly recommended to put them yourself.
>
> A good book to read about JavaScript is Cody Lindley's JavaScript Enlightenment and the work of Douglas Crockford such as JavaScript the good parts.
>
> Hope it helped.
>
> Renoir Boulanger
> https://renoirboulanger.com/#is
> ~
>
> On Jun 21, 2013 6:18 PM, "Bob Meetin" <bobm at dottedi.biz <mailto:bobm at dottedi.biz>> wrote:
>
>     I'm trying to employ javascript to obtain the local date and pull it into php. This partly works:
>
>     <script>
>     var currentTime = new Date()
>     var month = currentTime.getMonth()
>     var day = currentTime.getDate()
>     <?php $date = "<script>document.write(month + '/' + day)</script>"?>
>     <?php $jmonth = "<script>document.write(month)</script>"?>
>     </script>
>
>     <?php
>     echo "JavaScript Local Date: $date<br>";
>     $jmonth = chop($jmonth);
>     // $jmonth = (int)$jmonth; // fails
>     echo "jmonth: $jmonth<br>";
>     $pmonth = intval($jmonth); // no difference
>     $pmonth = $pmonth + 1;
>     echo "pmonth: $pmonth<br>";
>     if ( $pmonth > 5 ) { echo "Success"; } else { echo "Fail"; }
>     ?>
>


More information about the thelist mailing list