I'm using CodeIgniter, I want to offer discount for limited period of time. I don't know how to code it, would sessions be okay? But what if I want it only for first 15 mins, how can I manage that?
You can try something like this:
You might have to change your logic slightly to make this work.
<?php
function discount_period($field,$time)
{
$t = time();
$t0 = $_SESSION[$field];
$diff = $t - $t0;
if ($time > 1500 || !isset($t0))
{
return true;
}
else
{
$_SESSION[$field] = time();
}
}
/* Inside your header */
if(discount_period("user_time",1500))
{
session_unset();
session_destroy();
location("some-other-page.php");
exit;
}
?>
I don't know how you gonna do it but you can use this snippet of code as a reference.
<?php
$userLoggedInDate = new DateTime('now'); //The time user logged in. You can store it in the session variable the first time the user had logged in.
$promoDate = 'April 26, 2014';
$date = new DateTime($promoDate); //promo start date
$date2 = new DateTime($promoDate.'+15 min'); //add 15 mins to the date
var_dump($userLoggedInDate>$date2); //is logged in date greater than the promo end time?
var_dump($userLoggedInDate<$date2); //is logged in date less than the promo end time?