php timestamp to readable date not working as expected - php

Im trying to convert my timestamp to a readable date (going to be used in a sql query).
My code:
$date1 = date("d-m-Y",Input::get('van'));
return Input::get('van')." ".$date1;
The timestamp:
1451602800000
the result
15-12-1966
When i try this application the result of that timestamp is Thu, 31 Dec
2015 23:00:00 GMT
Which is what i was expecting.
What am i doing wrong that makes me get the wrong day-month year? the timestamp seems to be oke the code is the accepted answer here:
Adding:
date_default_timezone_set('UTC');
dosn't change anything

Remove three zeros from the right of your timestamp. A Unix timestamp is represented in seconds, not milliseconds.
$date1 = date('d-m-Y', Input::get('van') / 1000);
return Input::get('van') . ' ' . $date1;
See the time function for an example of an acceptable timestamp that can be used with the date function.

You probably use Laravel, so this package it integrated:
https://github.com/briannesbitt/Carbon
And try this tutorial to get up to speed:
https://scotch.io/tutorials/easier-datetime-in-laravel-and-php-with-carbon

Related

time() function in php says current time is 1970

I am running XAMPP on mac.
I have a very simple script:
<?php
echo time();
always returns time in the 1970.
example: 1475044574 (Edit: 1475137157)
I tried setting the timezone in php.ini and then calling date_default_timezone_get() and it returns the correct set value.
I tried adding SetEnv TZ MyTimezone to httpd.conf (at the bottom of the file) and it did not work.
I also tried setting the timezone in php with date_default_timezone_set() and though it sets successfully, time is still in 1970.
I tried the same script on MAMP and still the same problem.
Any suggestion is appreciated!
time function is returning proper unix timestamp you are doing mistake in converting time into proper format. 1475044574 means Wed, 28 Sep 2016 06:36:14 GMT which is correct.
You can convert time as you want. refer date formats.
check this sample code :
echo date('M j Y g:i A', 1475044574);
Bro, as per PHP manual php.net/manual/en/function.time.php time() function returns seconds not miliseconds. Please check timestamp your timestamp on this site epochconverter.com.
time() function returns current Unix timestamp. So, your 1475044574 is a timestamp.
Using date function you can see that this is a timestamp of (some variations with timezones, results may vary):
echo date('Y-m-d H:i:s', 1475044574);
// 2016-09-28 02:36:14
If you convert this timestamp to some string representation and get something like 00-00-1970 - you obviously do the conversion wrong.

Converting timstamp to date in php returns a wrong date?

I am trying to get the date from the following timestamp: 1410290399037
I have tried the following but I'm getting a wrong date
date('m-d-Y', 1410290399037); // output is: 04-28-46660
Also, I tried
$date = new DateTime();
$date->setTimestamp(1410290399037);
echo $date->format('m-d-Y'); // output is: 04-28-46660
I am getting the timestamp from a RESTFul web service. I am sure the timestamp is correct.
When I tested it in the following website http://www.epochconverter.com/, I got the right date
How can I get the right date value using PHP
Thanks
The problem is that the timestamp you are getting is in miliseconds, but PHP uses seconds. Simply divide what ever you get by 1000 and it will work.
$timstamp = 1410290399037;
$date = date('m-d-Y', $timstamp/1000);
echo $date;
The simple answer is that your timestamp is wrong.
I suggest trying an online unix timestamp converter to verify this such as
http://www.onlineconversion.com/unix_time.htm
Which yields Sat, 28 Apr 46660 13:03:57 GMT
Chomping off the last 4 numbers ( as if it were milliseconds )
gives Tue, 09 Sep 2014 19:19:59 GMT for 1410290399, is that closer to what you expect?

Get time at midnight(00:00:00) from a date

I am trying to convert a date to a timestamp at the exact midnight point.
To do this, I am using the following little function.
function converttotimestamp($date)
{
$date = str_replace('/', '-', $date);
$date = $date.' 00:00:00';
$date = DateTime::createFromFormat('m-d-Y H:i:s',$date);
return $date->getTimestamp();
}
So as you can see, I am attaching a midnight time at the end.
I tried using this as shown below
echo converttotimestamp('7/22/2014');
So as you would expect when you run this in a unix converter, you would get 1405987200.
But In my case it returns 1405976400 whicj translates to Mon, 21 Jul 2014 21:00:00.
Oh. I am in Kenya.
The reason you may be seeing a different time returned than the one you were expecting is likely because you haven't considered the relevant timezones. There are a couple different ways you can set the timezone. You can set it during runtime:
http://php.net/manual/en/function.date-default-timezone-set.php
You can set it in your PHP config file:
http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
Or you can set the timezone of your DateTime object:
http://php.net/manual/en/datetime.settimezone.php
Whenever you are converting between dates, you must consider the relevant timezone, as this is the only way for the system to determine how to switch between date formats, make comparisions and output specific dates and times. For example, if you want to convert a date and time to a timestamp, the system must know the timezone of the input date and time so it can convert properly. Take a look at strtotime:
http://us2.php.net/manual/en/function.strtotime.php
Unix timestamps are GMT timezone, so make sure you convert your datetimes accordingly. HTH.

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime

Past textual date to days (since) PHP

Hey guys,
how does one calculate the days past since a date like the one Twitter outputs in its API
eg:
Mon Jul 12 00:27:26 +0000 2010
to XXX
Can we do it with strtotime
Thanks guys,
Dex
Compatibility note: works only for PHP >= 5.3.0
Providing that the date format does not change, you can reverse the process (i.e. reverse timestamp -> string (on Twitters servers) to timestamp) using the exact date format. Using the table on the manual page of DateTime::createFromFormat:
<?php
$str = 'Mon Jul 12 00:27:26 +0000 2010';
$oDateTime = DateTime::createFromFormat('D M d H:i:s T Y', $str);
$unix_timestamp = $oDateTime->getTimestamp();
echo $unix_timestamp;
?>
Beware: On my machine, date('d-m-Y H:i:s', $unix_timestamp) differs two hours, the machines timezone is GMT+2.
To calculate the difference between in days between two Unix timestamps, use math (a day has 86400 seconds):
echo ($unix_timestamp1 - $unix_timestamp2) / 86400;
If you've two such dates, you can use DateTime::diff as suggested in the comments by Zerocrates. You've create two DateTime instances using DateTime::createFromFormat and invoke the DateTime::diff with two arguments passed, previously created DateTime instances. The returned DateInterval instance has a d property which contains the difference in days.
The other way would be using the getTimestamp method, doing the maths from the previous example.
References:
http://php.net/manual/en/datetime.createfromformat.php
http://php.net/manual/en/datetime.gettimestamp.php
http://www.php.net/manual/en/datetime.diff.php
http://www.php.net/manual/en/class.dateinterval.php
You can do it like that (where $your_date is the string you mentioned):
$diff = (time() - strtotime($your_date)) / (24*60*60);
In your case, when I did echo $diff; the output was (at the time I posted the answer):
321.85475694444
See more details for strtotime(), time() and date().
Hope this helped you.
You might find sth here:
performing datetime related operations in PHP
or in php manual there is a lot..
http://php.net/manual/en/function.date.php

Categories