Jump to content

Time based functions


sui_page

Recommended Posts

Heya guys! I'm using an open source platform called Ubercart which works with PHP and I've implemented the following code. Essentially I'm working towards dynamically changing the price of product after a certain time.

 

$dtA = new DateTime('01/15/2014 2:15PM');
 
if ( time() > $dtA ) {
  $item->price = $2.00;
}
else {
  $item->price = $item->price;
}
 
Problem is it doesn't work. In this instance I simply want the price of this item to be $2 if the current time has past '01/15/2014 2:15PM', which it has. Any guidance would be much appreciated. 
Link to comment
https://forums.phpfreaks.com/topic/285548-time-based-functions/
Share on other sites

 

You are trying to compare a time value with  DateTime object. Two options:

 $now = new DateTime(); if ($now > $dtA) { ...

or

if (time() > $dtA->getTimestamp()) { ...

 

I tried both these methods but couldn't get it to work. Would this be correct?

 

$dtA = new DateTime('01/15/2014 2:15PM');


if (time() > $dtA->getTimestamp(){
  $item->price = $item->price/2;
}
else {
  $item->price = $item->price;
}

Depends on what you are trying to achieve.

 

The else{} bit is totally redundant and as $dtA is set to a time in past then the current time will always be greater

 

Definitely the 'else'' is redundant. This is what I'm going for:

$dtA = new DateTime('01/15/2014 2:15PM'); 
$dtb = new DateTime('01/15/2014 4:15PM'); 

if (time() > $dtA->getTimestamp(){ 
  $ item->price = $item->price/2; 
}
else if(time() > $dtB->getTimestamp(){ 
  $item->price = $item->price/3;
 } 

So the price of this product will be half price for two hours and then be a third of the original price afterwards. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.