I am creating auction app where people can create their auctions on given date time from different locations.
I want to send notification to matched user for this auction but it's not working. Please have a look on below cron which will run every minute.
For exp: I have created a auction which will begin 2017-09-06 10:05
Am so before auction start I want to send notification to matched user on 2017-09-06 10:04
but it's not working.
I have checked and found my auction time being conflicted with server time. Is there any way to fix it.
I have setup time zone based on user location in my app
public function actionCron()
{
$currentTime = time();
$currentTimeFuture =$currentTime+90;
$tableName = \backend\models\Delivery::tableName();
$sql = "select id,user_id,auction_start_time from $tableName where status=1 and is_delete=0 and delivery_status=0 and auction_start_time between $currentTime and $currentTimeFuture";
$data = Yii::$app->getDb()->createCommand($sql)->queryAll();
foreach($data as $_dilevery){
// notification code here
}
}
The tricky issue is trying to manage different processes in different timezones. To keep things simple it is highly recommended that you store all of your dates/times in UTC and that your servers also run in UTC. This allows for easy date comparisons and you never have to worry about tricky things like daylight savings, etc.
To make this more user friendly you should then ask your user what timezone they are in when the user signs up for your application. You can store this timezone and then use it to simply format all dates/times in your application to make things easier for the user.
Here is an example of taking a UTC date/time and easily changing it for different timezones: https://eval.in/855883
// Store date/time as UTC
$database_datetime = '2017-09-05 22:00:00';
// Create a date/time object - TimeZone is UTC
$dt = new DateTime($database_datetime, new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'); echo "\n\n";
// Now change to user local date/time based on user timezone
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s'); echo "\n\n";
$dt->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $dt->format('Y-m-d H:i:s'); echo "\n\n";
Read more about PHP's DateTime
here: http://php.net/manual/en/class.datetime.php