Solved

Getting the incorrect date running filemtime using PHP to get the date modified of a file

Posted on 2014-02-17
5
1,181 Views
Last Modified: 2014-02-17
I setup a php script to pull the date modified date of a file and insert it into a database. When I run this script from a website the date comes back with the correct date. When I run it from Windows Scheduled task it come back with the incorrect date. Below is the script:

<?php
$con = mysql_connect('host','user','pass');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ats", $con);

mysql_query('TRUNCATE TABLE timeoff_file_date') or die(mysql_error());

    $filename = 'E:\wamp\www\timeoff\timeoff_data\Lawson.TimeAccrual.csv';
if (file_exists($filename)) {
    $date_modified = date ("Y-m-d H:i:s", filemtime($filename));
	echo $date_modified;
	$import_date = "INSERT INTO timeoff_file_date VALUES('".$date_modified."')";
    mysql_query($import_date) or die(mysql_error()); 
}

fclose($fp);
mysql_close($con);
echo "File Imported";

?>

Open in new window

0
Comment
Question by:ats2012
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
duncanb7 earned 250 total points
Comment Utility
When I run this script from a website the date comes back with the correct date

Do you know what is system linux or window on the website you got correct date ?
if it is linux , the date() might output the different from window.

http://hk2.php.net/function.date
The manual on date() points out two differences:

Timestamps used to range from 1970 to 2038 on Windows before PHP 5.1.0.
According to a User Contributed Comment, the Microseconds switch can't be used on Windows.

If the issue is not from system difference between linux and window, could you try
put date_default_timezone_set('yourtimezone') in your script and test it again on
website and window schedule ? where yourtimezone you can find in
php.net.


Hope understand your question completely, if not please pt it out.
Duncan
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
PHP and MySQL have different dates, independent with no locking between them.  PHP uses a "timezone" to format the Unix Timestamp for local use.  If this is set incorrectly (or the system date is off)  you may get faulty results.  This article explains the theory and practice:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You might also want to have an else clause for the if() statement on line 13.

And in unrelated, but important news, PHP is doing away with MySQL support.  The extension was deprecated at PHP 5.5, which is current.  An article that will help you map the familiar but obsolete MySQL functions to MySQLi or PDO is available here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Closing Comment

by:ats2012
Comment Utility
Added date_default_timezone_set('America/Chicago'); and it worked.

Thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Great!  Thanks for using EE, ~Ray
0
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
Thanks your pt  and have a nice day

Duncan
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now