chenci
chenci

Reputation: 113

How can i calculate date in php

Actually this is a bit tricky. I'm setting a food delivery system. When a buyer buys food from this webpage, he gets a mail that will says when the food will be delivered (an estimated time of delivery). If he buys between 00:00AM to 10:00AM, he gets his meal at 1230 PM. If he buy buys between 14:00 to 23:59, he gets his meal the next day at 1230PM. Needless to say, the food service is closed of taking order between 10:01 to 13:59. I already have the mailer system written, the only thing i don't have is the code to calculate when his food will be delivered. Can anybody help? Thanks

Upvotes: 0

Views: 177

Answers (2)

ghoti
ghoti

Reputation: 46856

I made this shell-executable. It assumes that the order time ($orders) is "now".

#!/usr/local/bin/php
<?php

$orders=array(
  "2012-02-21 09:21:00",
  "2012-02-21 10:45:00",
  "2012-02-21 14:10:00"
);

function ordertime($when) {
  $datearray=getdate(strtotime($when));
  if ($datearray['hours'] < 10) {
    // order ships today
    return strtotime("today 12:30");
  } else
  if ($datearray['hours'] < 14) {
    // we can't take this order
    return(FALSE);
  } else {
    // order ships tomorrow
    return strtotime("tomorrow 12:30");
  }
}

date_default_timezone_set("America/Toronto");

foreach ($orders as $oneorder) {
  $delivery=ordertime($oneorder);
  if ($delivery) {
    printf("%s: %s - %s\n", $oneorder, $delivery, strftime("%Y-%m-%d %T", $delivery));
  } else {
    printf("%s: INVALID ORDER TIME\n", $oneorder);
  }
}

When I run it, output looks like this:

ghoti@pc$ ./test.php
2012-02-21 09:21:00: 1329845400 - 2012-02-21 12:30:00
2012-02-21 10:45:00: INVALID ORDER TIME
2012-02-21 14:10:00: 1329931800 - 2012-02-22 12:30:00
ghoti@pc$ 

Is that what you were looking for?

Upvotes: 1

rajesh
rajesh

Reputation: 2523

you can do like this.

$time = date('H');
if($time <= 10){
  $delivery_time = time('d-m-Y').'12:30pm';
}else if($time >= 14){
  $delivery_time = date("d-m-Y",strtotime("+1 days")).'12:30pm';
}

Upvotes: 0

Related Questions