hi im having an error with my code can anyone help me try to fix my problem

the error is

Fatal error: Call to a member function diff() on a non-object in C:\xampp\htdocs\msicfinal\samp.php on line 12

and my code is

<?php
/*These look like they are sent via form submit to this program*/
$date1 = '2012-04-01'; // pick up date
$date2 = '2012-05-27'; // return date

$perday=2500;
$permonth=52500;
$perweek=15000;

$datetime1 = ($date1);
$datetime2 = ($date2);
$interval = $datetime1->diff($datetime2); // the math for the difference
$rest =  $interval->format('%d days');  // this may contain weeks
$week = intval($rest / 7);              // weeks extracted from the above var.                
$mon = $interval->format('%m');  // The mon from formatting
$day = intval($rest % 7);      // This line MUST STAY to figure number of days



$total = $mon * $permonth + $week * $perweek + $day * $perday;
echo $total . '<br />';
?> 

the purpose of this code is to compute the rate depending on the date

Recommended Answers

All 11 Replies

You're calling the diff function on a non object. Lines 10 and 11 should read:

$datetime1 = new DateTime($date1);
$datetime2 = new DateTime($date2);

ok but when i put new DateTime(); im having this error

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead' in C:\xampp\htdocs\msicfinal\samp.php:10 Stack trace: #0 C:\xampp\htdocs\msicfinal\samp.php(10): DateTime->__construct('2012-04-01') #1 {main} thrown in C:\xampp\htdocs\msicfinal\samp.php on line 10

any idea?

The answer's in the error message. You need to set a default timezone. It is typically best to add this to a config file for your website. E.g.:

date_default_timezone_set('Europe/London');

As blocblue says you can set timezone.
If you have access of php.ini, you can also set timezone in directly php.ini.

hi i got it already but the problem is im getting wrong output

here's the code

    <?php

    $timezone = "Asia/Shanghai";
    if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
    echo date('d-m-Y H:i');

    /*These look like they are sent via form submit to this program*/
    $date1 = '2012-06-01'; // pick up date
    $date2 = '2012-06-01'; // return date

    $perday=2500;
    $permonth=52500;
    $perweek=15000;


    $datetime1 = new DateTime($date1);
    $datetime2 = new DateTime($date2);
    $interval = $datetime1->diff($datetime2); // the math for the difference
    $rest =  $interval->format('%d days');  // this may contain weeks
    $week = intval($rest / 7);              // weeks extracted from the above var.                
    $mon = $interval->format('%m');  // The mon from formatting
    $day = intval($rest % 7);      // This line MUST STAY to figure number of days

    echo 'week '.$week . '<br />';  // The echos are for your benifit and should be commented out or erased
    echo 'day '.$day . '<br />';
    echo ' months '.$mon .'<br />';


    $total = $mon * $permonth + $week * $perweek + $day * $perday;
    echo $total . '<br />';
    ?> 

in the date part of this code i try to put the pick up date to 2012-06-01 the same with the return date..but im having a wrong output

it must be " 2500 "

but instead im getting this 0 for the total

22-06-2012 20:21
week 0
day 0
months 0
0

and also when i change the pick up date to 2012-06-01 and the return date to 2012-06-02

instead of getting "5000"

im getting

22-06-2012 20:28

week 0
day 2
months 0
2500

sorry if im bothering you it just that i really need a hand right now..

You're getting 0 for your first test, 2012-06-01 - 2012-06-01 because there is not difference between the two days.

You actually want the the number of days that have passed since the start date, including the start date itself. You could therefore try:

<?php

date_default_timezone_set('Europe/London');

$start_date = '2012-06-01';
$end_date = '2012-06-03';

$interval = strtotime($end_date) - strtotime($start_date);
$day = 60 * 60 * 24;
var_dump(($interval + $day) / $day); die;

im still not getting it the right output

here is the thing

if i put in date1 2012-06-01 and on date2=2012-06-01 it needed to have a default total and that total must be 2500..but if i change date2 to 2012-06-02 thats the time that it will need to add another 2500 to get the total of 5000

e.g

date1=2012-06-01
date2=2012-06-01

total=2500

e.g
date1=2012-06-01
date1=2012-06-02

total=5000

and so on until it get to

date1=2012-06-01
date1=2012-06-07

total=15000

again sorry if im pulling you to my problem..but help is really needed out here..thanks

Taking the code I posted previous a little further, and merging it with some of the code you posted previously, I came up with the code below.

It's worth noting that I have assumed 30 days in a single month.

<?php

date_default_timezone_set('Europe/London');

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 52500);
define('COST_PER_WEEK', 15000);
define('COST_PER_DAY', 2500);


$start_date = '2012-06-01';
$end_date = '2012-06-12';

echo calculate_cost($start_date, $end_date);

/**
 * Find total rental cost for period covered by start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return float
 */
function calculate_cost($start_date, $end_date)
{
    $interval = calculate_interval($start_date, $end_date);

    // Total months
    $months = floor($interval / DAYS_IN_MONTH);
    $interval = $interval % DAYS_IN_MONTH;

    // Total weeks
    $weeks = floor($interval / DAYS_IN_WEEK);
    $interval = $interval % DAYS_IN_WEEK;

    // Total days
    $days = $interval;

    // Calculate total cost
    return ($months * COST_PER_MONTH) + ($weeks * COST_PER_WEEK) + ($days * COST_PER_DAY);
}

/**
 * Find total number of days passed between start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return integer
 */
function calculate_interval($start_date, $end_date)
{
    return floor((strtotime($end_date) - strtotime($start_date) + SECONDS_IN_DAY) / SECONDS_IN_DAY);
}

When using this I get:

echo calculate_cost('2012-06-01', '2012-06-01');    // 2500
echo calculate_cost('2012-06-01', '2012-06-02');    // 5000
echo calculate_cost('2012-06-01', '2012-06-07');    // 15000
echo calculate_cost('2012-06-01', '2012-07-10');    // 75000

blocblue!!!thank you so much!!!!its perfect!your the best man..you save my ass out here..i'm in debt with you gain thank you so much....

Glad it works. Please just mark the thread as solved.

ok got it...i just need to incorporate it with the proj..again thanks alot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.