Except for date function, is there any function that can decode date? I think the real date should be in recent days, not a day in 2033.
<?php
$date = '1294605921000';
echo date("m-d-Y H:i:s", $date);
//11-08-2033 23:55:20
?>
No, but since that timestamp is likely in milliseconds instead of seconds, divide it by 1000:
echo date("m-d-Y H:i:s", floor($date / 1000));
The timestamp looks as though it has been generated by JavaScript, which uses milliseconds. If you divide the timestamp by 1000, you get the right date:
<?php
$date = 1294605921000/1000;
echo date("m-d-Y H:i:s", $date);
//01-09-2011 20:45:21
?>
I am assuming that this is an excel type date serial - but i dont recognise the example you put.
From the Manual http://uk.php.net/manual/en/ref.datetime.php
<?
function xl2timestamp($xl_date)
{
$timestamp = ($xl - 25569) * 86400;
return $timestamp;
}
?>
...
gmdate — Format a GMT/UTC date/time
gmstrftime — Format a GMT/UTC time/date according to locale settings
...
Or just take a look here... Old, but good PHP Manual
Related
I am trying to convert this time format - 1480550400000+0000 in Y/m/d date format using php date('Y/m/d',1480550400000+0000); but its not working. How can I make it work?
You timestamp has microseconds, so first remove it.
<?php
$timestamp = 1480550400000+0000;
$timestamp = intval($timestamp/1000);
$date = date("Y/m/d", $timestamp);
echo $date;
output: Check the live demo.
ei#localhost:~$ php test.php
2016/12/01
$dig_date= 1480550400000+0000;
$date = DateTime::createFromFormat('YmdGis', $dig_date);
echo $date->format('Y-m-d G:i:s');
Please note that the second parameter should be time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Looks like you have entered milliseconds instead.
PHP date()
string date ( string $format [, int $timestamp = time() ] )
timestamp
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Try this:
echo date('Y/m/d',1480550400+0000); // 2016/11/30
#Symplifys try this:
<?php
$date = date("Y-m-d", "1480550400000+0000");``
echo $date;
?>
Remember to put timestamp in double quotes.
Try this code at http://phpfiddle.org/
Solved I just divided this with 1000. $date = date("Y-m-d", $timestamp/1000); and it worked
Thanks
I want to get difference between 2 timestamps of format Y-m-d H:i:s in minutes in PHP.
The code used is,
$currentDate = date('Y-m-d H:i:s');
$userLastActivity = date($date);
$timeLapse = (($currentDate - $userLastActivity)/60);
Here, $date is obtained from database.
The value of $timeLapse is 0 in output. Please help.
Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.
$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);
You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion
Use strtotime to parse textual datetime into a Unix timestamp and substract $userLastActivity from $currentDate and divide by 60.
See if this helps -
<?php
$currentDate = strtotime(date('Y-m-d H:i:s'));
$date = "2016-10-11 02:40:50";
$userLastActivity = strtotime($date);
echo round(abs($currentDate - $userLastActivity) / 60). " minutes";
?>
For more details :strtotime
Change these 2 lines for a start, as I don't think dividing dates by 60 is gonna work to well.
$currentDate = time();
$userLastActivity = strtotime($date);
That way you have time stamps and not dates (string)
I tried to save time in format YYYY-mm-dd 23:59:59 to mysql database column with datetime. I don't understand why minutes and seconds are ignored always 00 ? Thank you very much for help.
PHP:
$time = strftime('%Y-%m-%d %H:%i:%s', time());
Output:
2014-07-16 11:00:00
You can simple use:
$time = date("Y-m-d H:i:s");
or even better use mysql NOW() function
you have to specify timezone as follows.
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions.
you can use mysql function
now()
You are looking for
$time = strftime('%Y-%m-%d %H:%M:%S', time());
from the documentation
%S Two digit representation of the second
%M Two digit representation of the minute 00 through 59
Also, what PHP version are you using? %i:%s should not be 00:00
I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.
I want to convert 12h time in php which is in this format: 05/31/2012 09:48 AM
to 24h time format : 2011-11-27 11:53:36
This is my code line that you can change:
$time= $_POST['time'];
You can use this:
// $time = 05/31/2012 09:48 AM
$time = $_POST['time'];
//output new time: 2012-05-31 09:48:00
$newTime = date("Y-m-d H:i:s", strtotime($time)) );
live example
Doc:
strtotime
date
http://psoug.org/snippet/Convert_12_to_24_hour_time_and_vice_versa_241.htm
You have to use strtotime like this:
$date="05/31/2012 09:48 AM";
echo date("Y-m-d H:i:s",strtotime($date));
go through below link for date functionality in PHP
http://php.net/manual/en/function.date.php
You can use strtotime and then date functions for that.
http://php.net/manual/en/function.date.php All the possible options are listed there.
The first argument to date is the format and the second is a timestamp (which you get with strtotime of the current date string you have).
date('Y-m-d H:i:s', strtotime($your_current_date_string));
The format is just off the top of my head and not exact. You can look up the format you need in the table in the link provided.