PHP filemtime time zoned - php

PHP coding question
How do I add DateTimeZone ('America/NewYork') to the PHP code at the end? I need my code to use filemtime. I included this footer.html in my main files so that all my website pages have the same footer. I also need the footer to filemtime the current file and not just header.html. If you know how to fix that too, please help me out.
<footer>
<div style="padding:0 30px">
<p>Validated by:</p>
<a href="https://validator.w3.org/check?uri=referer"><img src="https://www.w3.org/Icons/valid-xhtml11"
alt="Valid XHTML 1.1" height="31" width="88"></a>
<a href="https://jigsaw.w3.org/css-validator/check/referer"><img
src="https://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
<p>Last modified:
<?php echo " ".date("F j Y g:i a.", filemtime("header.html")); ?>
</p>
</div>
</footer>
I tried rewritting the whole thing but nothing worked. As in nothing showing up at all or the time is 0:0.0

What you want, if I understand you correctly is to output the timestamp of filemtime("header.html") in the timezone of America/NewYork.
This is how you would do that:
$timestamp = filemtime("header.html");
$date_time = new DateTime();
$date_time->setTimezone(new DateTimeZone("America/New_York"));
$date_time->setTimestamp($timestamp);
echo $date_time->format("F j Y g:i a.");
Note that it is America/New_York and not America/NewYork.
If you want to display the filemtime of the current file you can use __FILE__:
$timestamp = filemtime(__FILE__);

filemtime() produces a Unix time, which is not subject to time zones (it's a fixed moment in time). All PHP functions that operate on Unix time and require a time zone (e.g. date()) read it from the global value. Such value can be set with the date.timezone directive or the date_default_timezone_set() function.

Related

How do you get the "last modified" date from a file in the correct time zone?

So I am trying to get the Last Modified date of a file in PHP, I am using this:
$file = "my_file.txt";
$date = date("F j, Y H:i:s.", filemtime($file));
Now the problem is when I echo it, I do not get it in my timezone.
It comes up in UTC time, but I am in UTC-4 timezone.
(e1:) How do I get it to show my timezone, or even (e2:) display it in the timezone for whoever is viewing the webpage?
Example 1: Lets say I update the file at 1pm (UTC-4). Anyone looking at the webpage will see it displayed as 1pm (UTC-4) regardless of where they are located.
Example 2: Say I update the file at 1pm (UTC-4), and someone from the timezone UTC is looking at my webpage; they will see the time 5pm (UTC), because that's the time I uploaded it in their timezone.
Get the server time from PHP and store it in an HTML element. (There are other ways to pass the data, but this one is pretty easy to understand.) Here, I just used the time() function to grab the current time. Your filemtime method should return a timestamp if you omit the formatting options.
Then, grab the timestamp from the <div> with JS. Since the JS want milliseconds, multiply it by 1000. Then use toLocalString() to offset it to the user's local time.
<div id="timeholder" data-time="<?php echo json_encode(time()); ?>"></div>
<script type="text/javascript">
var date = new Date(document.getElementById('timeholder').dataset.time * 1000).toLocaleString();
console.log(date);
</script>
For Example 1:
Thanks to #thingEvery for doing the math on how much I needed to subtract from the current timestamp.
$filetime = date("U", filemtime($pfile));
$hoursToSubtract = 4;
$timeToSubtract = ($hoursToSubtract * 60 * 60);
$mytime = $filetime - $timeToSubtract;
# I needed to print it in two different formats,
# that's why I split it up
$time1 = date("F j, Y", $mytime);
$time2 = date("g:i A [e-4]", $mytime);

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

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.

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");

time() results a different time

I think it has something to do with PHP Timezones. My current date and time is 10:51 pm , and 7/15/2013 . (I am on windows and my bottom right is showing it :) )
I use the following php code
<?php
echo date("d/m/y : H:i:s", time());
?>
And the browser displayed: 15/07/13 : 19:06:15
(about 3 hours and 45 minutes early).
First Question: Why does it happen?
Second Question: If it is because PHP's default timezone is something else (mine is GMT 5:45), how can i edit the php's conf (or whatever) so that time() returns time for my timezone?
You can either set the time zone in your php.ini file or you can do it in the code:
<?php
date_default_timezone_set('America/Los_Angeles');
?>
http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
http://php.net/manual/en/function.date-default-timezone-set.php
http://www.php.net/manual/en/timezones.php
Also date_default_timezone_get() will show you what timezone you currently have set.
You should look date.timezone at your php.ini
Remember that PHP is server-side. The time it returns is bound to the server the code is running on. The php.ini setting is date.timezone. But you can use date_default_timezone_set to override the timezone specifically for your script. If you want to get the time in the timezone of the client, however, you have to use a client-side method like Javascript.

Creating a EDT (Eastern Daylight Time) - timestamp

I tried using the php function below to accomplish this, however, it returns a mounain time, timestamp. I was wondering if someone can help me understand how I change time zones, or if someone can help me correct this to EDT?
date("F j, Y, g:i a");
You can either set it locally in the script itself, by using the date_default_timezone_set() function:
http://php.net/manual/en/function.date-default-timezone-set.php
Or you can change it in your php.ini file, if you look for the line:
date.timezone
Edit: Here is a list of supported timezones, if you're not sure what yours should be called:
http://php.net/manual/en/timezones.php

Categories