Date format conversion problem

I want to get the user’s login local time
I coded this script in javascript which is located an the header of every page which is visible to users

	var currentTime = new Date();
// Get user current timestamp
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();
    var year = currentTime.getFullYear();
    var month = currentTime.getMonth() + 1;
    var date = currentTime.getDate();

userCurrentTime = year+'-'+month+'-'+ date+' '+ hours+':'+minutes+':'+seconds; 

console.log() gives me the time stamp format that I want:
2024-2-17 1:41:8

Yet, when I convert the javascript variable to a PHP variable (so I can insert it itto a DB table)

$currenttime = "<script>document.write(currentTime);</script>";

I get a new date format

Sat Feb 17 2024 01:41:08 GMT+0200 (Israel Standard Time)

I don’t want to use a cookie because then I will need the users consent for cookies usage
Every thing I trued to do to change the format resulted in error messages, blank page, and the date 1970/1/1 00:00:00

What is the correct way to change the PHP date format to’Y-m-d H:m:i’ ?

Note that the GDPR requirements for data storage are not limited to Cookies. It’s about what data you collect, not how you collect it.

You havent used a PHP date format. You’re using a Javascript output.

Btw I cannot image this is the format you want. You can nearly do nothing with such a format.

Be sure to use

2024-02-17 01:41:08

Instead or even better


Because this formats can be used to:

  • stored in a database
  • sorted by date
  • search for day/month/year

Above you put your formatted time into variable “userCurrentTime”.

$currenttime = “< script>document.write(currentTime);< /script>”;

Then here you put the original time “currentTime” into the PHP variable. They are two different formats. Try putting your formatted time “userCurrentTime” into the PHP variable.