Peter Jackson
Peter Jackson

Reputation: 63

php - if date older than 6 months ago

I want the below code to check the created date of a customer, if they have been a customer for 6 months the price goes up as it was a promotion. I tried the below with no joy, I think as its just using the month and not taking the year into account. For example if a customer came onboard in September the 6 month would be the next year and it never change.

Thanks

$created_date = date('m',strtotime($customer_order['created_at']));
            $current = date("m");

            $curmonth = date("m");

            $ordermonth = date("m",strtotime($udata['created_date']));
            $m_dff = $curmonth - $ordermonth;

            //print_r($m_dff."<br>");
            if($m_dff > 6){
                $unitcost = 19.99;

            }
            else{
                $unitcost = 14.99;
            }

Upvotes: 1

Views: 2962

Answers (3)

Miza Aida
Miza Aida

Reputation: 11

$currentorder = date ('Y-m-d');
$createdaccount =  date('Y-m-d', strtotime("+6 months", 
strtotime($customer_order['created_at'])));

if($currentorder>=$createdaccount)
{
    $unitcost = 19.99;
}
else
{
    $unitcost = 14.99;
 }

Upvotes: 1

Matt Garrod
Matt Garrod

Reputation: 878

strtotime() can be used more effectively than in your example, the following should do the trick

if(time() > strtotime($customer_order['created_at'] . ' +6 months')) {
    $unitcost = 19.99;
} else {
    $unitcost = 14.99;
}

Upvotes: 1

Nitin
Nitin

Reputation: 916

See if this works using DateTime objects to compare.

//set a general time zone at top of script
date_default_timezone_set('Europe/Amsterdam');

// if created_at is a valid dateTime string
$created = new \DateTime($customer_order['created_at']);

$current = new \DateTime();

$sixMonth = $created->add(new \DateInterval(‘P6M’));

if ($created < $current) {
    // if created + 6 months is older than current
    $price = 19.99;
} else {
    $price = 14.99;
}

For more info see: https://secure.php.net/manual/en/book.datetime.php

Upvotes: 0

Related Questions