Comparing timestamp error

Hi all

I am hoping someone can help with this problem.

I have a system which compares a number of stockfiles from suppliers FTP servers… and depending on whether they are newer than the current data it will update our systems when needed.

The suppliers files are uploaded to our FTP server. My system stores the initial datafiles timestamp in a SQL database when it imports the data to our system, then hourly it compares the timestamp of the suppliers file to the timestamp in the SQL database… if its different it deletes that suppliers data from the Stocklist database, replaces it and updates the timestamp database with the timestamp of the newly imported data. I do this test as there are so many files, if they were all updated together it takes about 10 minutes for the script to run!

This works seamlessly and is run hourly on an Ubuntu server as a cron job.

However I have a new supplier who sends two data files, one contains their stock listings and the second contains product pricing. When I import these into the Stocklist database I have to ‘join’ data from both files.

However I want to compare the timestamps on both files and if either have changed for the update process to start. But for some reason I cannot get this to work - it always shows the timestamps as different and runs a delete & update process.

I am using identical scripts for all the other suppliers and it is faultless, so I cannot work out why this wont work :frowning:

This is how I am getting the timestamp from the datafile uploaded by the supplier;


	$file1_timestamp = (string)date (filemtime('/var/www/clients/client1/web1/web/distcheck/datafiles/supplier/FILE1.TXT'));
	$file2_timestamp = (string)date (filemtime('/var/www/clients/client1/web1/web/distcheck/datafiles/supplier/FILE2.TXT'));

This is how I am getting the timestamp from the SQL database (which stores the timestamp of the data file when the last update took place);

	$sql_supplier_file1="SELECT `timestamp` FROM `timestamp` WHERE `disti` = 'Supplier1'";
	$result=mysqli_query($mysqli,$sql_supplier_file1);
	$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
	$timestamp_supplier_file1 = $row["timestamp"];

	$sql_supplier_file2="SELECT `timestamp` FROM `timestamp` WHERE `disti` = 'Supplier2'";
	$result=mysqli_query($mysqli,$sql_supplier_file2);
	$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
	$timestamp_supplier_file2 = $row["timestamp"];

These all work perfecty as an example, the results for these variables are;

	$file1_timestamp = 1713292194
	$file2_timestamp = 1713373268

	$timestamp_supplier_file1 = 1713292194
	$timestamp_supplier_file2 = 1713373268

So, as both the files have the same timestamp in the timestamp SQL database and the actual files, I want to compare the two and for nothing to happen… this is the formula I am using;

	if (($file1_timestamp == $timestamp_supplier_file1) AND ($file2_timestamp == $timestamp_supplier_file2)) {
		echo "Timestamps matches - no update required</br>";
	}else{
		echo "Timestamps dont match - update required</br>"; 
		}

But, everytime the script runs, whether the timestamps match or not, I get the message that the timestamps dont match!!

I also tried this alternative script (which doesnt work!!);

if ($file1_timestamp == $timestamp_supplier_file1) {
	if ($file2_timestamp == $timestamp_supplier_file2) {
		echo "Timestamps match - no update required</br>";
	}else{
		echo "Timestamps dont match - update required</br>"; 
		}
	}else{
		echo "Timestamps dont match - update required</br>";
		}

Please note all timestamps are standard UNIX so they can be compared.

So, if anyone can help I would be very grateful.

The date() usage in these lines is incorrect. The first parameter of the date() call is a format string that determines what format to use. When you supply a value that doesn’t contain any formatting characters, you get that literal value as the result. filemtime() directly returns a unix timestamp. Just use filemtime().

What does using var_dump() on all 4 of the values show?

I don’t know if the posted code is just for testing or if you are actually writing out hard-coded, bespoke logic for every supplier? If you are writing out this code/query for every supplier, you need to instead use a data-driven design, where you have a data structure (array, database table) that controls what general purpose code does. You would then loop over this defining data to get and dynamically operate on all the data, without repeating the code/query for every file/supplier.

Thanks @phdr

The script is not using an array as I am a novice at PHP and this is a live system which has been hard coded over the years - initially there were only one or two suppliers and as more have come on board I have just written a script for each supplier and the script has just grown over the years.

The variable results are;

$file1_timestamp = "1713292194"
$file2_timestamp = "1713373268" 
$timestamp_supplier_file1 = "1713292194" 
$timestamp_supplier_file2 = "1713373268" 

That’s not what the output from var_dump() statements would look like. It would include the data type and for stings, the length. We/I are trying to help you find out why the comparisons don’t work and need the actual information about the values.

Sorry @phdr - I did edit it to make it ‘human readable’… I have never used var_dump() before, so this is the code I have added;

echo "file1_timestamp = ".var_dump($file1_timestamp)."</br>";
echo "file2_timestamp = ".var_dump($file2_timestamp)."</br>";
echo "timestamp_supplier_file1 = ".var_dump($timestamp_supplier_file1)."</br>";
echo "timestamp_supplier_file1 = ".var_dump($timestamp_supplier_file1)."</br>";

and the output is;

string(10) "1713292194" file1_timestamp =
string(10) "1713373268" file2_timestamp =
string(10) "1713292194" timestamp_supplier_file1 =
string(10) "1713373268" timestamp_supplier_file2 =

Strangely I added a test;


if ($file1_timestamp == $timestamp_supplier_file1) {
	echo "Supplier File 1 matches - update NOT needed!</br>";
} else {
	echo "File1 does not match - update IS needed!</br>";
}

if ($file2_timestamp == timestamp_supplier_file2) {
	echo "Supplier File 2 matches - update NOT needed!</br>";
} else {
	echo "File2 does not match - update IS needed!</br>";
}

and get this output;

Supplier File 1 matches - update NOT needed!
Supplier File 2 matches - update NOT needed!

So, it looks like it may actually be working now… I am going to change one of the Database time stamps to check it correctly identifies an update is required and will let you know the outcome.

Sponsor our Newsletter | Privacy Policy | Terms of Service