time() function in php says current time is 1970 - php

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.

Related

php - Different behaviour of `DateTimeImmutable` for the same datetime value in different format

Let's suppose we have this date 1972-12-31 23:59:59, if we get the TimeStamp for it from DateTimeImmutable object we will get this:
$formattedDate = '1972-12-31 23:59:59';
$ts = (new DateTimeImmutable($formatedDate))->getTimestamp(); // <- 94690799
The problem that if you try to revers the conversion, so it becomes from timestamp into formatted date:
$ts = 94690799;
$formattedDate =
(new DateTimeImmutable(sprintf('#%s', $ts)))->format('Y-m-d H:i:s'); // <- 1972-12-31 22:59:59
There is an hour gone in the second way.
So the million dolor question would be, which one of those timing is corresponding to the correct time?
Is this a bug? Or am I messing something in here?
When you create a DateTime object from a formatted string, it is created in your server's default timezone (see date_default_timezone_get). But Unix timestamps don't have a timezone - they're always in UTC. So if you write:
(new DateTimeImmutable('1972-12-31 23:59:59'))->getTimestamp();
then what you're really asking PHP is "How many seconds after 1970 in UTC was it, when it was that date + time in my current timezone". In your case, the server looks like it is running one hour ahead of UTC, hence the difference.
Crucially, when you do the inverse and create a DateTime object from a timestamp, the object's timezone is always set to UTC. There's a brief note about it on this manual page.
If you set the default timezone to UTC before running the code, you'll see that the output matches. I've added an example here: https://3v4l.org/2Rfp3

strtotime converting on wrong timezone

I am facing the following problem when converting a date value using strtotime().
If I do: strtotime('1/1/2019') the output would be 1546293600 -> 31.12.2018 # 10:00pm (UTC)
If I do: date_default_timezone_get() the output is Europe/Bucharest
In my php.ini file (of my hosting account), the timezone is set to date.timezone="Europe/Helsinki"
I have following two questions.
Shouldn't date_default_timezone_get() output what it is configured
in my hosting accounts php.ini file? (the
date_default_timezone_set() is not being used)?
Why the strtotime() conversion result is on GMT-2 timezone? It is
my understanding that if there is no timestamp supplied, the
conversion should be done on the current time (which should be
GMT+2, either Helsinki or Bucharest)?
If I do date_default_timezone_set('UTC') the conversion result of strtotime('1/1/2019) is done correctly (on GMT+2).
Thank you.
There may be some reasons why this is happening and we can't tell you why, because we don't know your application, since it may somewhere change the time zone at runtime.
Instead you should stop using strtotime and embrace object oriented DateTime functions.
Using DateTime you can define your date along with its time zone:
// local time
$date = new \DateTimeImmutable('1/1/2019', new \DateTimeZone('Europe/Bucharest'));
echo $date->format('U > d.m.Y (e)') , '<br>';
// convert to UTC
echo $date->setTimeZone(new \DateTimeZone('UTC'))->format('U > d.m.Y (e)');
The library is quite powerful and much more verbose than operating on strings and unix timestamps.
Provide hour and timezone to the function using the full IS0 8601 format 2019-01-01T12:00:00+00:00. That way you will get the correct timestamp in UTC. If you won't use the hour I recommend to use 12 AM.
In the following example I will use a numeric date array to build this string
// [Y,m,d]
$date = [2019,1,1];
for($i=1; $i<3; $i++){
$date[$i] = str_pad($date[$i],2,'0',STR_PAD_LEFT);
}
$t1 = strtotime(implode('-',$date).'T12:00:00+00:00');
echo date('r',$t1);
//=>Tue, 01 Jan 2019 12:00:00 +0000

php timestamp to readable date not working as expected

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

PHP date showing wrong time despite the timestamp being correct

I'm having a problem with the PHP date function which I've never had a problem with before.
The timestamp is entirely correct, however for some bizarre reason date() is outputting a time which does not correspond.
I have the following timestamp (and this is definitely the correct one - when I echo it out onto the page, as well as in the database, it shows as being correct):
464400
Yet when I use the following line of code:
<?php echo date("H:i",$timestamp); ?>
I'm getting a time of 4 am? If I paste the timestamp into a timestamp converter website, then it shows the time should in fact be 9am.
I'm completely stuck, this has never happened to me before & this problem has only just come up recently - the code hasn't been changed and everything seemed to be working correctly before.
Does anyone have any experience with this issue? Any help would be appreciated.
That time stamp is is 9am GMT timezone, if you are in another timezone then you will need to adjust it accordingly.
http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('Europe/London');
or even better in your php.ini
http://php.net/manual/en/datetime.configuration.php
date.timezone="Europe/London"
Or you can use more specifically GMT instead of Europe/London (which has DST)
try this method will work
for time zone
http://php.net/manual/en/timezones.php
code
<?php
date_default_timezone_set('Asia/Kolkata');
$dt2=date("Y-m-d H:i:s");
echo $dt2;
?>
try this
// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
Try this converter too http://freeonlinetools24.com/
For time zone: https://www.php.net/manual/en/timezones.php
date_default_timezone_set('America/Chicago');
echo date("m/d/Y h:i:s A");

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.

Categories