?
Solved

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

Posted on 2014-02-17
5
Medium Priority
?
1,433 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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 111

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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