konratnox
1/19/2016 - 1:07 PM

Таймер(заготовка)

Таймер(заготовка)

Делаем аякс запрос, устанавливаем интервал повтора каждую секунду

<?php
$cur_date = new Datetime();
$mess_about_sales = new Datetime('18.01.2016 14:00:00');
$saleStart = new Datetime('21.01.2016 00:00:01');
$tillStart = date_diff($cur_date, $mess_about_sales);
$tillSaleStart = date_diff($cur_date, $saleStart);
$salesEnd = new Datetime('23.01.2016 00:00:01');
$result['cur_date'] = $cur_date->format('H:i:s');
$result['mess_about_sales'] = $mess_about_sales->format('H:i:s');
if($cur_date < $salesEnd) {
	if($cur_date > $mess_about_sales) {
		$result['timer'] = 1;
		$result['sales_start_h'] = $tillSaleStart->format('%h');
		$result['sales_start_min'] = $tillSaleStart->format('%i');
		$tillSalesEnd = $cur_date->diff($salesEnd);
		$daysTillEnd = $tillSalesEnd->format('%a');
		$result['sales_end_d'] = $daysTillEnd;
		if( $daysTillEnd > 0 ) {
			$result['sales_end_h'] = $tillSalesEnd->format('%h') + ($daysTillEnd*24);
		} else {
			$result['sales_end_h'] = $tillSalesEnd->format('%h');
		}
		
		$result['sales_end_min'] = $tillSalesEnd->format('%i');

	} else {
		$result['timer'] = 0;
		$timeTillStartD = $tillStart->format('%a');
		$timeTillStartH = $tillStart->format('%h');
		$timeTillStartm = $tillStart->format('%i');
		$result['till_start_days_mes'] = $timeTillStartD;
		$result['till_start_hours_mes'] = $timeTillStartH;
		$result['till_start_min_mes'] = $timeTillStartm;

	}
} else {
	$result['salesOver'] = 1;
}
echo json_encode($result);

?>
var timer = function() {
		$.ajax({
			url: '/scripts/timer.php',
			type: 'post',
			dataType: 'json',
			data: {},
		})
		.done(function(data) {
			console.log("success");
			console.log(data);
		})
		.fail(function() {
			console.log("error");
		})
		.always(function() {
			console.log("complete");
		});
		
	};
	setInterval(timer, 1000);