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
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;
}
Link to comment
Share on other sites

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. 

Edited by sui_page
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.