Cron jobs


#1

ok, i don’t know if this is a problem with the script, or a problem elsewhere

The script runs via a cron job every hour. The script is supposed to check to see if its between 9am and 5pm in the users area.

When i run the script manually its fine, when ran by a cron it runs via the server time

[code]<?php

// Connect to the database
include(“db_config.php”);

// Get users time zone
$sql = “SELECT ID, timezone, name, personnelpayment FROM members ORDER BY ID ASC”;
$result = mysql_query($sql) or die(sql_error());
while($row = mysql_fetch_array($result)) {
if (substr($row[‘timezone’], 0, 1) == ‘-’)
{
$time = (gmmktime() - (floatval(substr($row[‘timezone’],1)) * 3600));
}
else
{
$time = (gmmktime() + (floatval($row[‘timezone’]) * 3600));
}
// Update Factories, Warehuouses, Stores and Items
$time = gmdate(“H:i:s”, $time);
print $time." - “.$row[‘name’].”\n";
if($time >= “09:00:00” && $time <= “17:00:00”) {
// Include Approved
include(“approved.php”);
// Include Items
include(“items.php”);
// Include Warehouse
include(“warehouse.php”);
// Include Transit
include(“transit.php”);
// Include Stores
include(“stores.php”);
// Include Wages
include(“wages.php”);
}
}

?> [/code]For some reason its running by the server time, not the time specified in the script, since the one that ran earlier says

[quote]04:30:02 - desbrina
No
04:30:02 - Mnemosyne
No
04:30:02 - CrimsonSeal
No[/quote]
running it manually gives

[quote]12:31:51 - desbrina
Yes
Approved ran
items ran
warehouse ran
transit ran
stores ran
wages ran
12:31:51 - Mnemosyne
Yes
Approved ran
items ran
warehouse ran
transit ran
stores ran
wages ran
12:31:51 - CrimsonSeal
Yes
Approved ran
items ran
warehouse ran
transit ran
stores ran
wages ran[/quote]
anyone any ideas?


#2

Cron jobs get a clean environment so maybe it has something to do with that, maybe you set TZ somewhere? Also what PHP are you running? On my server /usr/local/bin/php is v4.4.9 and /usr/local/php5/bin/php is 5.2.6 whcih might make a difference.
Try this code.[code]<?php

// Constants

define(‘DATE_SERVER’, ‘D Y-m-d H:i:s T’);
define(‘DATE_LOCAL’, ‘H:i:s’);
define(‘DATE_UNIX_EPOCH’, ‘U’);

// Header

header(‘Content-Type: text/plain’);
$server_timezone_str = date_default_timezone_get();
$server_datetime = date_create(‘now’, new DateTimezone($server_timezone_str));
$server_datetime_str = date_format($server_datetime, DATE_SERVER);
echo “Running at $server_datetime_str ($server_timezone_str)\n\n”;

// row fetched from database
$row = array(
‘ID’ => ‘id’,
‘timezone’ => ‘-6’,
‘name’ => ‘John Doe’,
‘personnelpayment’ => 42.00
);

// Normalize timezone value assuming database uses -12 to 12 integers
// Should use strings instead of offsets. For some reason PHP expects
// the offset sign to be reversed, must be a bug.
$user_gmt_offset_kludge = $row[‘timezone’] * -1;
$user_timezone_str = ‘Etc/GMT’ . ($user_gmt_offset_kludge > 0 ? ‘+’ : ‘’) . $user_gmt_offset_kludge;

// Calculate UNIX epoch timestamps

$user_timezone = new DateTimezone($user_timezone_str);
// Get current date and time local to user
$user_datetime = date_create(‘now’, $user_timezone);
// Format it
$user_datetime_str = date_format($user_datetime, DATE_LOCAL . ’ ’ . $user_gmt_offset);
// UNIX epoch value
$time_now = date_format($user_datetime, DATE_UNIX_EPOCH);
// Set time to 9:00 local to user
date_time_set($user_datetime, 9, 0, 0);
// UNIX epcoh value
$time_start = date_format($user_datetime, DATE_UNIX_EPOCH);
// Set time to 17:00 local to user
date_time_set($user_datetime, 17, 0, 0);
// UNIX epoch value
$time_end = date_format($user_datetime, DATE_UNIX_EPOCH);

// Run task

echo "$user_datetime_str - " . $row[‘name’] . “\n”;
if ($time_now >= $time_start && $time_now <= $time_end)
{
echo “\tYes\n”;
}
else {
echo “\tNo\n”;
}[/code]
:cool: openvein.org -//-