Convert Epoch Time to Date PHP

Convert Epoch Time to Date PHP

Fixed it using substr($epoch, 0, 10) and then used the date function for anyone wondering about the 13 digit epoch times.

Here is a sample code:

echo date("Y-m-d H:i:s", substr("1477020641000", 0, 10));
// Result: 2016-10-20 20:30:41

PHP: Convert epoch to MySQL DateTime format

PHP

date("Y-m-d H:i:s",$epochTS);

MySQL

FROM_UNIXTIME(timestampColumn)

Nothing more to it

Converting a UNIX Timestamp to Formatted Date String

Try gmdate like this:

<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>

Converting Epoch time to an H:mm timestamp in PHP

As others already noticed, this is not really any standard epoch time but simply number of seconds since midnight.

Looking at your example, you only need hours and minutes (rounded up). This will give you those:

$h = (int)($number / 3600);
$m = ceil(($number - $h * 3600) / 60);
$result = sprintf('%d:%02d', $h, $m);

Dividing number by 3600 (60 for seconds * 60 for minutes) will give you number of hours. Using your example of 3672 this will give you 1.

To get minutes you just remove hours to get seconds (72) and then divide that by 60 to get minutes (1 minutes and 12 seconds). Since your example specifies 1:02 is result, you can simply take next upper integer (2).

At end result is 1:02, as you specified.

Unix epoch time converts to human readable incorrectly with PHP

The mistake is using m in the second date call. m is month, minutes is i.

echo date('Y.m.d', 1630440104).' at '.date('H:i:s', 1630440104);

or

echo date('Y.m.d \a\t H:i:s', 1630440104);

Just a side note, date second parameter accepts an int not a string in declare(strict_types=1); a TypeError would occur.

You can find the available formats under DateTime::format docs page.

PHP - Convert date and time into Epoch

Use strtotime function

echo strtotime("11/22/2018 01:16:14 AM");

How to convert a date YYYY-MM-DD to epoch in PHP

Perhaps this answers your question

http://www.epochconverter.com/programming/functions-php.php

Here is the content of the link:

There are many options:

  1. Using 'strtotime':

strtotime parses most English language date texts to epoch/Unix Time.

echo strtotime("15 November 2012");
// ... or ...
echo strtotime("2012/11/15");
// ... or ...
echo strtotime("+10 days"); // 10 days from now

It's important to check if the conversion was successful:

// PHP 5.1.0 or higher, earlier versions check: strtotime($string)) === -1
if ((strtotime("this is no date")) === false) {
echo 'failed';
}

2. Using the DateTime class:

The PHP 5 DateTime class is nicer to use:

// object oriented
$date = new DateTime('01/15/2010'); // format: MM/DD/YYYY
echo $date->format('U');

// or procedural
$date = date_create('01/15/2010');
echo date_format($date, 'U');

The date format 'U' converts the date to a UNIX timestamp.


  1. Using 'mktime':

This version is more of a hassle but works on any PHP version.

// PHP 5.1+ 
date_default_timezone_set('UTC'); // optional
mktime ( $hour, $minute, $second, $month, $day, $year );

// before PHP 5.1
mktime ( $hour, $minute, $second, $month, $day, $year, $is_dst );
// $is_dst : 1 = daylight savings time (DST), 0 = no DST , -1 (default) = auto

// example: generate epoch for Jan 1, 2000 (all PHP versions)
echo mktime(0, 0, 0, 1, 1, 2000);

Converting Epoch timestamp to DateTime

As explained by this answer, the Chrome timestamp is not equal to Unix epoch time. This is why you're not getting the results you expect from such methods. It's actually microseconds since the 1st Jan, 1601 (as opposed to Unix epoch time's seconds since 1st Jan, 1970).

You can test your WebKit timestamp here, where you'll see it returns Tuesday, 6 August 2019 10:57:48 (UTC).

So to convert this in code, we should first subtract the difference between 1970 and 1601 (in microseconds) and then divde the value by 1 million to get seconds (C# solution):

public static DateTime ConvertWebKitTime(long webkitEpoch)
{
const long epochDifferenceMicroseconds = 11644473600000000; // difference in microseconds between 1601 and 1970
var epoch = (webkitEpoch - epochDifferenceMicroseconds) / 1000000; // adjust to seconds since 1st Jan 1970
return DateTimeOffset.FromUnixTimeSeconds(epoch).UtcDateTime; // convert to datetime
}


Related Topics



Leave a reply



Submit