A
I've done the task quite elegantly.Since JS can't define a local time zone, but can format unixTimeStamp In local time, we'll have to forget the transfer of the formatted timeline from the server and hand it over to the client. unixTimeStamp for subsequent localization by JS.
To make local time filling less visible to the user, as well as the code on the server, do not change, together with unixTimeStamp We'll always give time to Greenwich.Order That's right.1 In the PHP, the time is always Greenwich, using it. gmdate()and appointed dateTime component, contentsinnerHTMLwhich will need to be formatted.
In one of the attributes of this element data-date-time♪ data-date♪ data-timeplaced unixTimeStamp depending on what we need to format it.<p>Дата со временем: <span class="dateTime" data-date-time="<?=$date?>"><?=gmdate('d.m.Y (H:i)',$date)?></p>
<p>Дата: <span class="dateTime" data-date="<?=$date?>"><?=gmdate('d.m.Y',$date)?></p>
<p>Время: <span class="dateTime" data-time="<?=$date?>"><?=gmdate('H:i',$date)?></p>
2 In the numerical prototype JS, we define suitable methods for time formatting and define a convenient function dateTime()which, once the document is downloaded, will receive all elements that need to be formatted and replace their time/date/date over time, whichever is the place unixTimeStamp Greenwich for local time. Call for our function. dateTime() after the completion of the downloading of the document.<script type="application/javascript">
<!--
'use strict';
Number.prototype.time=function()
{
return new Date(this*1000).toLocaleString('uk',{'hour':'numeric','minute':'numeric'});
}
Number.prototype.date=function()
{
return new Date(this*1000).toLocaleString('uk',{'day':'numeric','month':'numeric','year':'numeric'});
}
Number.prototype.dateTime=function()
{
return this.date()+' ('+this.time()+')';
}
function dateTime()
{
let doc=document.getElementsByClassName('dateTime');
for(let l=doc.length,i=0;l>i;++i)
if(typeof doc[i].dataset.dateTime=='string')
doc[i].innerHTML=parseInt(doc[i].dataset.dateTime).dateTime();
else if(typeof doc[i].dataset.date=='string')
doc[i].innerHTML=parseInt(doc[i].dataset.date).date();
else if(typeof doc[i].dataset.time=='string')
doc[i].innerHTML=parseInt(doc[i].dataset.time).time();
}
window.addEventListener('load',dateTime);
//-->
</script>