Solved

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

Posted on 2014-02-17
5
1,324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 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