I got the following format of date from an API:
1468102548
I'm trying to convert this date to a normal human readable format, however I can't get it to work.
Here's what I've tried so far:
$creation_date = "1468102548";
$input1 = $creation_date / 1000;
$newDate_creation_date = date("Y-m-d", $input1); // Output: 1970-01-17 (It's not right)
That timestamp is not in milliseconds so dividing it by 1000 is skewing your date.
$creation_date = new DateTime('#1468102548');
echo $creation_date->format('Y-m-d'); // Outputs 2016-07-09
Demo
Related
I tried to calculate date in php based on this formula:
<?PHP
$lunarMonth = 29.530589;
function getFib($n)
{
return round(pow((sqrt(5)+1)/2, $n) / sqrt(5));
}
$fibbonaciNumber = getFib(2);
$addToDate = sqrt($fibbonaciNumber*$lunarMonth);
$date = '2017-01-01';
$timeStamp = strtotime($date);
$calucatedDate = $timeStamp+$addToDate;
echo 'Date: '.date('Y-m-d', $calucatedDate).'<br>';
?>
But it dosen't work, i cant calculate new date by using timeStamp function. What should i try?
Sample output is:
addToDate: 24.902657870195
timeStemp: 1483311600
CalculateDate: 1483311624.9027
Date: 2017-01-02
This show always the same date, no matter what CalculateDate date is.
I current using excel to calculate this:
=DATE(YEAR(A4)+0#MONTH(A4)+0#DAY(A4)+(29,530589*(SQRT(1)))),
for example input: 2009-02-18, should output: 2009-03-18, for fibonacci number 1
I need to move base date based on calucation $fibbonaciNumber*$lunarMonth
Thanks
I got messages by gmail api php,here I have tried to get date by getInternalDate is output long number!So I want to change user readable date but I can't when format by date() using php!!
$single_message = $service->users_messages->get('me', $message_id, $optParamsGet2);
$date = $single_message->getInternalDate();//'1464161738000'
var_dump(date("Y",$date));// 1956 ,should be 2016
Seems that getInternalDate return a value in miliseconds instead of second.
so you just have to divide it by 1000 and then use the date function.
$date = $single_message->getInternalDate() / 1000;
var_dump(date("Y-m-d H:i:s", $date));
Divide $date by 1000 :) the internalDate is in ms.
I'm required to take a date in the format 'Y-z' which is year-doy (e.g, 2013-146) and convert that into a unix time stamp to be store into a database.
The issue i have is that i input 2013-146 and turn it into a DateTime Object. then when i output this date in unix or 'Y-m-d' format i get 2013-5-27 not 2013-5-26 which is the correct day.
You can verify the DOY on this NASA website and this NOAA website.
Summary:
--I have the date: '2013-146'
--Using DateTime::createFromFormat and echoing using 'Y-m-d' and 'Y-z' i get: 2013-5-27 and 2013-146 respectively.
--This does not agree with the NASA website I listed and is offset by one day can anyone verify that I'm not losing my mind?
Here is the code you can test:
<?php
date_default_timezone_set('America/Chicago');
$year = 2013; //where this outputs a simple year 'CCYY'
$day = 146; //where this provides the day of year
$format = 'Y-z'; //specifying what format i'm creating the datetime with
$date = $year.'-'.$day; //formatting the strings to the above $format
$timezone = new DateTimeZone('America/Chicago'); //specify the timezone
$fileDateStore = DateTime::createFromFormat($format, $date, $timezone);//, $timezone); //create the DateTime object
$fileDateString = date_format($fileDateStore,"Y-m-d"); //format it so strtotime() can read it
$fileDate = strtotime($fileDateString); //finally create the Unix Timestamp for the date.
$newfileDOY = date_format($fileDateStore,"Y-z");
echo 'newfileDOY = '.$newfileDOY.', ';
echo 'date = '.$date.', ';
echo 'fileDateString = '.$fileDateString.', ';
echo 'fileDate = '.$fileDate.PHP_EOL;
?>
The problem is than z format in PHP begins with 0 and not with 1.
Look at: http://www.php.net/manual/en/function.date.php
z: The day of the year (starting from 0)
How do i convert this excel dataserial value 41225 back to date format 12-Nov-2012 using phpexcel and code igniter?
I have tried the following but it didn't work.
$G74 = $objPHPExcel->getActiveSheet()->getCell('B6')->getValue();
Dates in Excel are stored as number of days since 1st Jan 1900, except there is an off by one error due to 1900 not being a leap year. You can create therefore a DateTime object with this hack (valid for dates from 1st March 1900 onwards):
$n = 41225;
$dateTime = new DateTime("1899-12-30 + $n days");
You can format the DateTime Object with something like:
echo $dateTime->format("d M Y");
If you want to include the time as well as the date, multiply by 86400 (the number of seconds in a day) to get seconds since 1st Jan 1900 before you convert:
$n = 42898.35416666;
$dateTime = new DateTime("1899-12-30 + ". round($n * 86400) . " seconds");
Using the getFormattedValue() method rather than getValue() might help if the cell has a format mask that formats it as a date. getValue() returns a raw value, which (in this case) is the Excel serialized number.
Otherwise, the ExcelToPHP() or ExcelToPHPObject() methods in the PHPExcel_Shared_Date class should do the job of returning a unix timestamp or a PHP DateTime object that you can then format however you wish
I had to convert a dateserial number in decimal format ie: 42898.35416666 and the DateTime object does not take decimals.
Based on rjmunro's answer, and animuson's answer on how to convert decimals to hours and minutes, here is a solution for decimal Excel dates.
function dateSerialToDateTime($dateserial) {
$arrDate = explode(".", $dateserial);
$n = $arrDate[0];
$decimal = "." . $arrDate[1]; //decimal amount of seconds
$duration = 86400 * $decimal; //number of seconds in a day * decimal
$dateTime = new DateTime("1899-12-30 + $n days");
$converted = $dateTime->format("Y-m-d") . " " . gmdate("H:i:s", $duration);
return $converted;
}
$dateserial = 42898.35416666;
die(dateSerialToDateTime($dateserial));
//returns 2017-06-12 08:29:59
How to get millisecond between two DateTime objects?
$date = new DateTime();
$date2 = new DateTime("1990-08-07 08:44");
I tried to follow the comment below, but I got an error.
$stime = new DateTime($startTime->format("d-m-Y H:i:s"));
$etime = new DateTime($endTime->format("d-m-Y H:i:s"));
$millisec = $etime->getTimestamp() - $stime->getTimestamp();`
I get the error
Call to undefined method DateTime::getTimestamp()
In the strict sense, you can't.
It's because the smallest unit of time for the DateTime class is a second.
If you need a measurement containing milliseconds then use microtime()
Edit:
On the other hand if you simply want to get the interval in milliseconds between two ISO-8601 datetimes then one possible solution would be
function millisecsBetween($dateOne, $dateTwo, $abs = true) {
$func = $abs ? 'abs' : 'intval';
return $func(strtotime($dateOne) - strtotime($dateTwo)) * 1000;
}
Beware that by default the above function returns absolute difference. If you want to know whether the first date is earlier or not then set the third argument to false.
// Outputs 60000
echo millisecsBetween("2010-10-26 20:30", "2010-10-26 20:31");
// Outputs -60000 indicating that the first argument is an earlier date
echo millisecsBetween("2010-10-26 20:30", "2010-10-26 20:31", false);
On systems where the size of time datatype is 32 bits, such as Windows7 or earlier, millisecsBetween is only good for dates between 1970-01-01 00:00:00 and 2038-01-19 03:14:07 (see Year 2038 problem).
Sorry to digg out an old question, but I've found a way to get the milliseconds timestamp out of a DateTime object:
function dateTimeToMilliseconds(\DateTime $dateTime)
{
$secs = $dateTime->getTimestamp(); // Gets the seconds
$millisecs = $secs*1000; // Converted to milliseconds
$millisecs += $dateTime->format("u")/1000; // Microseconds converted to seconds
return $millisecs;
}
It requires however that your DateTime object contains the microseconds (u in the format):
$date_str = "20:46:00.588";
$date = DateTime::createFromFormat("H:i:s.u", $date_str);
This is working only since PHP 5.2 hence the microseconds support to DateTime has been added then.
With this function, your code would become the following :
$date_str = "1990-08-07 20:46:00.588";
$date1 = DateTime::createFromFormat("Y-m-d H:i:s.u", $date_str);
$msNow = (int)microtime(true)*1000;
echo $msNow - dateTimeToMilliseconds($date1);
DateTime supports microseconds since 5.2.2. This is mentioned in the documentation for the date function, but bears repeating here. You can create a DateTime with fractional seconds and retrieve that value using the 'u' format string.
<?php
// Instantiate a DateTime with microseconds.
$d = new DateTime('2011-01-01T15:03:01.012345Z');
// Output the microseconds.
echo $d->format('u'); // 012345
// Output the date with microseconds.
echo $d->format('Y-m-d\TH:i:s.u'); // 2011-01-01T15:03:01.012345
// Unix Format
echo "<br>d2: ". $d->format('U.u');
function get_data_unix_ms($data){
$d = new DateTime($data);
$new_data = $d->format('U.u');
return $new_data;
}
function get_date_diff_ms($date1, $date2)
{
$d1 = new DateTime($date1);
$new_d1 = $d1->format('U.u');
$d2 = new DateTime($date2);
$new_d2 = $d2->format('U.u');
$diff = abs($new_d1 - $new_d2);
return $diff;
}
https://www.php.net/manual/en/class.datetime.php
Here's a function to do that + tests.
https://gist.github.com/vudaltsov/0bb623b9e2817d6ce359eb88cfbf229d
DateTime dates are only stored as whole seconds. If you still need the number of milliseconds between two DateTime dates, then you can use getTimestamp() to get each time in seconds (then get the difference and turn it into milliseconds):
$seconds_diff = $date2.getTimestamp() - $date.getTimestamp()
$milliseconds_diff = $seconds_diff * 1000