Solved

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

Posted on 2014-02-17
5
1,207 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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

15 Experts available now in Live!

Get 1:1 Help Now