Search code examples
phptimetimestampunix-timestamp

Modify a timestamp to another timestamp


I have a unix timestamp as follows: 1561420800 which results in Monday, June 24, 2019 8:00:00 PM.

What I want is to use the timestamp so i can recreate a new timestamp and obtain the month and year and then results in Monday, June 24, 2019 00:00:00 AM

You can see the hours, minutes, and seconds are all 00 and its in the AM

If its possible in the DateTime object id prefer that. I cannot find any examples on the api https://www.php.net/manual/en/datetime.modify.php


Solution

  • Using DateTime you can do the following...

    $timestamp = 1561420800;
    $dt = new DateTime('@' . $timestamp);
    $dt->settime(0,0,0);
    print_r($dt);
    

    which shows the time set as

    DateTime Object
    (
        [date] => 2019-06-25 00:00:00.000000
        [timezone_type] => 1
        [timezone] => +00:00
    )