Solved

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

Posted on 2014-02-17
5
1,262 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
ID: 39865264
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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39865427
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
ID: 39865465
Added date_default_timezone_set('America/Chicago'); and it worked.

Thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39865483
Great!  Thanks for using EE, ~Ray
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39865495
Thanks your pt  and have a nice day

Duncan
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

825 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