Php epoch to date

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.

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

FROM_UNIXTIME(timestampColumn)

Converting a UNIX Timestamp to Formatted Date String

$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);
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

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

Here is the content of the link:

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:

Читайте также:  Last insert rowid python

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.

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
>

Источник

PHP Epoch Converter and Date/Time Routines

How to convert epoch / UNIX timestamps to normal readable date/time using PHP 7.

Getting current epoch time in PHP

Time returns an integer with the current epoch:

time() // current Unix timestamp microtime(true) // microtime returns timestamp with microseconds (param: true=float, false=string) 

Convert from epoch to human-readable date in PHP

1. Use the ‘date’ function.

$epoch = 1483228800; echo date('r', $epoch); // output as RFC 2822 date - returns local time echo gmdate('r', $epoch); // returns GMT/UTC time: Sun, 01 Jan 2017 00:00:00 +0000 

You can use the time zone code below (date_default_timezone_set) to switch the time zone of the input date.

2. Use the DateTime class.

$epoch = 1483228800; $dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00 

In the examples above «r» and «Y-m-d H:i:s» are PHP date formats, other examples:

Format Output
r Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date)
c 2017-03-15T12:00:00+01:00 (ISO 8601 date)
M/d/Y Mar/15/2017
d-m-Y 15-03-2017
Y-m-d H:i:s 2017-03-15 12:00:00

Convert from human-readable date to epoch in PHP

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

echo strtotime("15 November 2017"); // . or . echo strtotime("2017/11/15"); // . or . echo strtotime("+10 days"); // 10 days from now 
if ((strtotime("this is no date")) === false)

2. Using the DateTime class:

The PHP DateTime class is nicer to use:

// object oriented $date = new DateTime('01/15/2017'); // format: MM/DD/YYYY echo $date->format('U'); // or procedural $date = date_create('01/15/2017'); echo date_format($date, 'U'); 

The date format ‘U’ converts the date to a UNIX timestamp.

Читайте также:  Формы

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 ); // example: generate epoch for Jan 1, 2000 (all PHP versions) echo mktime(0, 0, 0, 1, 1, 2000); 

All these PHP routines can’t handle dates before 13 December 1901.

Set your timezone

Use date_default_timezone_set to set/overrule your timezone.
The PHP time zone handling takes care of daylight saving times (list of available time zones).

date_default_timezone_set('Europe/Amsterdam'); date_default_timezone_set('America/New York'); date_default_timezone_set('EST'); date_default_timezone_set('UTC'); 

Convert date/time to another time zone

$TimeStr="2017-01-01 12:00:00"; $TimeZoneNameFrom="UTC"; $TimeZoneNameTo="Europe/Amsterdam"; echo date_create($TimeStr, new DateTimeZone($TimeZoneNameFrom)) ->setTimezone(new DateTimeZone($TimeZoneNameTo))->format("Y-m-d H:i:s"); 

Adding or subtracting years/months to a date

With strtotime you can easily add or subtract years/months/days/hours/minutes/seconds to a date.

$currentDate = time(); // get current date echo "It is now: ".date("Y-m-d H:i:s", $currentDate)."\n "; $date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +1 year"); // add 1 year to current date echo "Date in epoch: ".$date."\n "; echo "Readable date: ".date("Y-m-d H:i:s",$date)."\n "; 
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +1 month"); // add 1 month to a date $date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +6 months"); // add 6 months $date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +1 day"); // add 1 day $date = strtotime(date("Y-m-d H:i:s", $currentDate) . " -12 hours"); // subtract 12 hours $date = strtotime(date("Y-m-d H:i:s", $currentDate) . " -1 day -12 hours"); // subtract 1 day and 12 hours 

Источник

Convert epoch time to date php

And a quote from there: Also note that the current timezone is getting ignored, which you can also see in the manual: Solution 2: Printing date and time is correct. The variable returns different dates, but the time is always the same .

DateTime->format(epoch) returning the wrong date

I am working on a project and I am having an issue formatting an epoch time to a human readable time.

I have the following epoch time 1428512160 and when I put this through epochconverter.com I get the human time of 08/04/2015 17:56:00 GMT+1:00 DST as expected.

I then use the following code in order to perform the conversion from the epoch time to a human date time.

$dt = new DateTime($supportDetails["Reported"]); $reportedTimeString = $dt->format('d-m-Y H:i:s'); 

$supportDetails[Reported] is the epoch time (I’ve printed it so I know it’s correct).

The result I get back however is 08-04-2160 14:28:51 .

You need to add an @ for the timestamp in the DateTime class, like this:

$dt = new DateTime("@" . $supportDetails["Reported"]); //^ See here 

You can also see this in the manual. And a quote from there:

Unix Timestamp «@» «-«? 2+ «@1215282385»

Also note that the current timezone is getting ignored, which you can also see in the manual:

Note: The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800 ) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Printing date and time is correct. Its based on what GMT you have set in your PHP. If you printing with GMT you will get required result.

Читайте также:  Use javascript with html5

$reportedTimeString = date(«d-m-Y H:i:s», $supportDetails[«Reported»]);

$date = new DateTime(); $date->setTimestamp($supportDetails["Reported"]); $reportedTimeString = $date->format("d-m-Y H:i:s"); 

The problem I see is with your formatting.

If you look at PHP’s date function you can see that you just need to write each portion of the desired date & time into a string.

The following formatting gives the same output you were looking for:

$dt = new DateTime($supportDetails[«Reported»]); $reportedTimeString = $dt->format(‘d/m/Y H:i:s \G\M\TP T’);

Php date time to epoch Code Example, $format = «Y-m-d h:i:sa»; // format for date output. 4. $formatted_date = date($format,$timestamp)); // convert timestamp to format.

Change to hardcoded epoch time in variable?

I have a variable called $end_date which gives me the date in epoch format like this: 1539129600 . This translates to Wednesday 10 October 2018 00:00:00 .

The variable returns different dates, but the time is always the same 00:00:00 . Is it possible to take this variable, set the time to 23:59:59 and save it back to a new variable called $end_date_time containing the same date and the new time 23:59:59 in epoch format.

You can do this with the DateTime class, using setTimestamp to convert the timestamp and then setTime to set the time of day, finally getting the new timestamp with format :

$end_date = 1539129600; $end_of_day = new DateTime(null, new DateTimeZone('UTC')); $end_of_day->setTimestamp($end_date); echo $end_of_day->format('Y-m-d H:i:s') . "\n"; $end_of_day->setTime(23,59,59); echo $end_of_day->format('Y-m-d H:i:s') . "\n"; echo (int)$end_of_day->format('U'); 
2018-10-10 00:00:00 2018-10-10 23:59:59 1539215999 

Convert timestamp to readable date/time in PHP, Solution: This can be achieved with the help of date() function, which is an inbuilt function in PHP can be used to format the timestamp given

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

As per title: how to convert a string date (YYYY-MM-DD) to epoch (seconds since 01-01-1970) in PHP

Perhaps this answers your question

Here is the content of the link:

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)

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.

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); 
$date = '2013-03-13'; $dt = new DateTime($date); echo $dt->getTimestamp(); 

Use the strtotime() function:

use strtotime() it provides you Unix time stamp starting from 01-01-1970

Источник

Оцените статью