-3

I need to get user client's date and convert it into PHP vars.

I know it's a bit hacky, but I am able to get the date from the user client into PHP

$today_js = '<script>document.write(new Date());</script>';

echo $today_js;
//  Mon Oct 28 2019 12:12:20 GMT-0500 (Central Daylight Time)

So I was hoping to convert the string to manipulate further in PHP like this:

$today_js = '<script>document.write(new Date());</script>';
$userdate = strtotime($today_js);
$today_dt = getDate($userdate);
$year     = $today_dt['year'];
$month    = $today_dt['mon'];
$day      = $today_dt['mday'];

However I'm not getting desired outcome. The year is 1970 and both month and date are 1.

The question is not about front- back-end coding. I am getting the following PHP string: "Mon Oct 28 2019 12:12:20 GMT-0500 (Central Daylight Time)" just need the rest of my PHP code to work with it.

What am I missing?

santa
  • 12,234
  • 49
  • 155
  • 255
  • why not ajax it? – treyBake Oct 28 '19 at 17:25
  • The code you're providing is not "getting the date from the user client into PHP". That's not how it works. JS is client-side, PHP is server-side. PHP is simply echoing the script tag as markup and it is still executed by the client. – Constantin Groß Oct 28 '19 at 17:28
  • I am getting PHP string with correct date. How do I get it to work with strtotime()? – santa Oct 28 '19 at 17:33
  • 1
    @santa I bet you're not. That would be some kind of black magic. Please show us the result of `echo htmlentities($today_js);` then. – Constantin Groß Oct 28 '19 at 17:34
  • Please drop this code: document.write(new Date());'; $userdate = strtotime($today_js); $today_dt = getDate($userdate); $year = $today_dt['year']; $month = $today_dt['mon']; $day = $today_dt['mday']; echo '$today_js: '.$today_js.' | $userdate: '.$userdate.' | $date: '.$date.' | $year: '.$year.' | $month: '.$month.' | $day: '.$day; ?> here: http://phpfiddle.org/lite – santa Oct 28 '19 at 17:39
  • I did, and it's throwing errors. If you try `document.write(new Date());'; echo htmlentities($today_js); ?>`, you'll see that the variable is not actually containing the date/time string - because how could it? How do you expect the server-side code to be able to read the client-side code?` – Constantin Groß Oct 28 '19 at 17:44
  • If you create a string like: `$today_js = ''`, then `$today_js` will contain the literal string `''`, not the result of the JS code. PHP simply doesn't execute JS. – M. Eriksson Oct 28 '19 at 17:45

3 Answers3

0

What you are trying to achieve is impossible the way you are doing it.

PHP is running on the server, and your code will execute when a new request from the client hits it. This means this code will never go to the client and then back to the server.

The way you can achieve what you're looking for is by sending a new request (AJAX, fetch, you pick) from the client with the user date to the server. You will need an endpoint /get-user-date for example, which will get that data and then do whatever you need to do with it, and return the result. Which will then be used by JS in the client to show whatever you want to show.

Sorry if this explanation is very broad, but it's hard to explain such a concept in few words.

Obed Parlapiano
  • 3,226
  • 3
  • 21
  • 39
  • Is this not a valid PHP string: " Mon Oct 28 2019 12:12:20 GMT-0500 (Central Daylight Time)"? If yes, then I need the rest of PHP to work with it. – santa Oct 28 '19 at 17:35
  • @santa - Yes, it's a valid PHP string. So is `foobar` but that's besides the point since PHP will never get the result of the js code in the string the way you've done it for the reasons this answer and all the comments already mentioned. JS is executed after PHP and PHP doesn't execute JS. So when you do: `$foo = ''`, that will be the literal string (including ` – M. Eriksson Oct 28 '19 at 17:49
  • @santa you are focusing on the wrong thing. The way you are trying to solve the problem isn't the right one. ` – Obed Parlapiano Oct 28 '19 at 17:52
0

get date in fornat 2019-10-10

$date  = date("Y-m-d");

get year 2019

$year = date("Y")

> get month 10

$month = date("m");

get day 28

$day = date("d");

>  Pega data completa ex: 2019-10-29 12:12:20

date("Y-m-d H:i:s")

visit : https://www.php.net/manual/pt_BR/function.date.php

Danilo Santos
  • 392
  • 3
  • 11
  • Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Oct 28 '19 at 23:31
0

PHP runs on the server-side, so that it will never read the client / browser time directly.

If you need to read the client time, read it using javascript:

<script>
date = new Date();
window.location.href = "http://somesite.somedomain/index.php?date="+date.toUTCString();
</script>

and read it from your php script:

if (isset($_GET['date'])) {
    $today_js = str_replace('%20', ' ', $_GET['date']);

    $userdate = strtotime($today_js);
    $today_dt = getDate($userdate);
    $year     = $today_dt['year'];
    $month    = $today_dt['mon'];
    $day      = $today_dt['mday'];  

    echo $year.' - '.$month.' - '.$day;
}

or you can use ajax instead of GET variable to pass the value.

chris
  • 89
  • 3