Skip to content
Advertisement

setTime() replacement in php [closed]

I want to convert below javascript code to PHP

<script>    
     noon = new Date();
     alert(noon);
     noon.setTime(1641951202187.3433);
     alert(noon);   
</script>

returning value Wed Jan 12 2022 07:03:22 GMT+0530 (India Standard Time)

As in javascript setTime() function Add 1641951202187.3433 milliseconds to January 1, 1970. But i am unable to find such function in php or solution

Advertisement

Answer

First of all, you need to convert milliseconds to seconds and then use the date() function with the following format:

$mili = 1641951202187.3433;
$sec = $mili /1000;
echo date('M-m-Y H:i:s',$sec);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement