translate Last Modified Date of a file from UNIX to local timezone - php

Using PHP, I am trying to retrieve the Last Modified Date of files, exactly as they appear in Windows Explorer. The PHP code looks like this: date('m/d/Y h:i:s A', filemtime($file));. Here is an example of a file in Windows Explorer and the corresponding output from the PHP script:
Clearly, the Last Modified Date from the PHP script does not match the date being displayed in Windows Explorer. The subject file was created in the Central Standard Timezone where daylight savings time is practiced. Is there a way to convert the UNIX date returned by PHP's filemtime() to the local timezone?

I think the method you need here is probably date_default_timezone_set. There's a list of all the timezones that are supported by PHP, but the one you'll need for central time is America/Chicago.
So, your code should look something like this:
<?php
date_default_timezone_set('America/Chicago');
$fileDate = date('m/d/Y h:i:s A', filemtime($file));
?>
Here's the documentation on the timezone set method.

Related

php strtotime function showing 1 hour more

I am trying to convert 1355657846 and 1355677646 unix timestamp to Y-M-D H:i:s format.
The problem is in H:i:s . It should be
11:37:24 and 17:07:26 respectively but it is showing 12:37:24 and 18:07:26.
<?php
echo date('Y-m-d H:i:s','1355657846');//2012-12-16 12:37:26,must be 11:37:26
echo date('Y-m-d H:i:s','1355677646');//2012-12-16 18:07:26,must be 17:07:26
?>
It should be 11:37 and 17:07 because I checked it in unix timestamp conversion
and also it is the time I had received mail in gmail account. And I got these unix timestamp from gmail( using php imap function...$overview->udate)
I am testing this on local xampp server.
Can anyone suggest me where I am going wrong here?
PS: I checked related question in stackoverflow, but here I want to convert timestamp to datetime, which I think should be constant irrespective of timezone.
It is definetily the timezone setting in your local web server.
Check the php.ini for date.timezone value.
It also may be overridden by htaccess file or you PHP script.
strtotime was included once DateTime was created. The parsing is dependant of the server default timezone.
If you want get your date with the good timezone, there are two ways :
default_timezone_set('Europe/Paris');//for example
or :
$date = DateTime::createFromFormat('U',$timestamp,new DateTimezone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s);
you can find more informations here .
This is probably due to the TimeZone setting. Learn about PHP date_default_timezone_set() and make sure your local time zone matches the timezone you really want. All Unix Timestamps are the same world-wide. But the local DATETIME values vary around the globe. This article may be visible to you (hope so):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Best regards, ~Ray

Change the simple date time format to show time zone

I have a simple date time in the format 10/20 4:30PM. I want it to display in the format
10/24 1:30PM -007. I am saying date('Y-m-d H:i:sT', $time_recieved) assuming T is for timezone. But It is still not giving the time zone. What might be the reason? Is there any other format I am missing?
What you want is O not T I believe.
Check the PHP date page for all the format listings possible.
http://php.net/manual/en/function.date.php
You can have a look at the official manual of PHP : date ();
http://www.php.net/manual/en/function.date.php
I tested that and I used on my server with :
$time = time();
echo date('Y-m-d H:i:s T', $time);
it shows the timezone perfectly. Check $time_received, it might be an erroneous number, try with local time with timeĀ“() function to ensure that it's OK.
Good luck

file_put_contents create file with wrong timestamp

I am having an issue with "file_put_contents" when i run this code:
file_put_contents("/var/www/html/storage/views/temp", "helllo world");
It saves the file with a time 1hr in the future.
So where dose it get the time?
I have set php.ini date.timezone = "Europe/London" (as per system timezone) and that seems not to help
php.ini date.timezone option specifies which timezone PHP will use internally.
Most Unix/Linux system use UTC as hardware and then set the system clock to a given timezone.
You end-up with three different timezone, but it should be totally transparent.
Your problem may be due by the fact that your system timezone is different than your php timezone.
To check your system timezone, you can do date +%Z
Depending on your need you will either have to change your PHP timezone according to your system timezone, or convert the date of your timestamp to manage your file.
Excepting you've a good reason to do it, I suggest to adjust your php timezone with your system timezone.
file_put_contents gives the file the server time. That means, when looking at the timestamp of the file, the timezone of the server must be taken into account and must eventually be transformed to the timezone of the client.
On my linux machine date outputs: Thu Sep 20 11:23:28 CEST 2012 and thats the timestamp for the file also. When in CST timezone, you have to substract 7 hours from the the timestamp to get the creation time in CST.
You need to handle the timestamp value correctly, when you use it. Examine this:
<?php
$file = __DIR__ . '/test.dat';
file_put_contents($file, "hello world");
print(gmdate('Y-m-d H:i:s T', filectime($file)) . "\n");
print(date('Y-m-d H:i:s T', filectime($file)) . "\n");
Output:
2012-09-20 09:29:17 GMT
2012-09-20 11:29:17 CEST
Depending on the specific date method you use, you can determine the desired output.

PHP: strtotime formatting

I am trying to put a readable time and date as part of a file name (in php). I am having all kinds of trouble with this and was hoping someone could help. I have tried several different recommendations that I have read around the internet (plus I read the manual) but I really haven't gotten anything to work right. Right now I have this:
$Time=strtotime("now");
$date=DateTime::createFromFormat('m/d/Y H:i:s', '7/24/2012 14:40:30');
$date_readable=$date->$Timestamp();
At that point I then add $date_readable to a file name. It compiles and runs but it doesn't format the date at all. It still gives it as a timestamp.
Any suggestions on how to make this work?
you can do it with simple date function for example
$time = strtotime("now");
$formatDate = date('F jS, Y h:i:s A', $time);
echo $formatDate;
this will print something like
July 25th, 2012 1:02:29 am
DateTime class is more powerful then using simple date function, as DateTime class offers powerful API's plus it is object oriented. however for simple date conversions i would stick to php's date function. as that could do my purpose.
for more formatting option have a look at this link http://www.php.net/manual/en/function.date.php#refsect1-function.date-parameters

Editing server time for a PHP script

I have changed the way i save the time when a log is submitted to the DB by using the 'datetime' stamp in MySQL.
Im saving it in this format date("Y-m-d G:i:s"); which outputs it like so: 2012-02-22 20:20:01
The only problem is im in the UK, and my hosting/server isnt.
I have found out how to edit the time to bring it inline with UK time by doing this:
date = date("Y-m-d G:i:s", strtotime("+14"));
So now i have the time right i thought i would be ok, but now i have noticed that the date is not inline with UK date/time.
If i did a submission now it would display: 2012-02-22 20:25:36 when the time/date is actually 2012-02-23 20:20:01.
So it looks like that because my script is 14 hours behind, its knocking the date out to.
Is there a way i can fix it ?
Thanks, Sam!
include this in your program
date_default_timezone_set('Europe/London');
Add this one line in your php.ini file:
date.timezone = "Europe/London"
Now, anytime you use date or time functions, it will be adjusted to what you have specified.
For a complete list of timezones in Europe:
http://www.php.net/manual/en/timezones.europe.php
Utilizing the PHP Datetime module: Supported Timezones
$tz = new DateTimeZone('Pacific/Kiritimati'); // Change this to YOUR needed Timezone
$datetime_GMT = new DateTime(strtotime($server_or_SQL_date));
$datetime_GMT->setTimezone($tz);
$datetime_GMT->format("Y-m-d G:i:s");
echo $datetime_GMT;

Categories