What I read is that date_diff was not working in 5.2, etc and have to have 5.3 or later to make it work. I have 5.3.5 and always errors!!
Simple code of:
Code:
$cur_date = date("y/m/d");
$max_date = get_max_date(); // Get largest finish date from table
echo "C=> $cur_date M=> $max_date <br>";
$dif_date = date_diff($max_date, $cur_date);
echo "Report Days=> $dif_date <br>";
should work but always give error of:
Quote:
Warning: date_diff() expects parameter 1 to be DateTime, string given in /var/www/intranet/wordpress/schedule_test/functions/functions.php on line 64
No this is not a WordPress app, just resides under that dir.
All help appreciated!
Thanks!
OMR
As it says right there in the error message, it expects a DateTime object but you are passing a string. Your database data is a string. I think if you convert it even just to an integer then it would work (but maybe not? I don't have PHP 5.3+ so I can't test).
Try this and see if it helps:
PHP Code:
$cur_date = date("y/m/d");
$max_date = get_max_date(); // Get largest finish date from table
echo "C=> $cur_date M=> $max_date <br>";
$max_date = intval($max_date);
$dif_date = date_diff($max_date, $cur_date);
echo "Report Days=> $dif_date <br>";
Or if that doesn't work I found this on PHP.net:
PHP Code:
function diff($start,$end = false) {
/*
* For this function, i have used the native functions of PHP. It calculates the difference between two timestamp.
*
* Author: Toine
*
* I provide more details and more function on my website
*/
// Checks $start and $end format (timestamp only for more simplicity and portability)
if(!$end) { $end = time(); }
if(!is_numeric($start) || !is_numeric($end)) { return false; }
// Convert $start and $end into EN format (ISO 8601)
$start = date('Y-m-d H:i:s',$start);
$end = date('Y-m-d H:i:s',$end);
$d_start = new DateTime($start);
$d_end = new DateTime($end);
$diff = $d_start->diff($d_end);
// return all data
$this->year = $diff->format('%y');
$this->month = $diff->format('%m');
$this->day = $diff->format('%d');
$this->hour = $diff->format('%h');
$this->min = $diff->format('%i');
$this->sec = $diff->format('%s');
return true;
}
Toine (contact at toine dot pro) 26-Aug-2011 02:22
This is a very simple function to calculate the difference between two timestamp values.
PHP Code:
<?php
function diff($start,$end = false) {
/*
* For this function, i have used the native functions of PHP. It calculates the difference between two timestamp.
*
* Author: Toine
*
* I provide more details and more function on my website
*/
// Checks $start and $end format (timestamp only for more simplicity and portability)
if(!$end) { $end = time(); }
if(!is_numeric($start) || !is_numeric($end)) { return false; }
// Convert $start and $end into EN format (ISO 8601)
$start = date('Y-m-d H:i:s',$start);
$end = date('Y-m-d H:i:s',$end);
$d_start = new DateTime($start);
$d_end = new DateTime($end);
$diff = $d_start->diff($d_end);
// return all data
$this->year = $diff->format('%y');
$this->month = $diff->format('%m');
$this->day = $diff->format('%d');
$this->hour = $diff->format('%h');
$this->min = $diff->format('%i');
$this->sec = $diff->format('%s');
return true;
}
/*
* How use it?
*
* Call your php class (myClass for this example) and use the function :
*/
$start = strtotime('1985/02/09 13:54:17');
$end = strtotime('2012/12/12 17:30:21');
$myClass = new myClass();
$myClass->Diff($start,$end);
// Display result
echo 'Year: '.$myClass->Year;
echo '<br />Month: '.$myClass->Month;
echo '<br />Day: '.$myClass->Day;
echo '<br />Hour: '.$myClass->Hour;
echo '<br />Min: '.$myClass->Min;
echo '<br />Sec: '.$myClass->Sec;
// Display only month for all duration
$month = ($myClass->Year * 12) + $myClass->Month;
echo '<br />Total month: '.$month;
// if you want you can use this function without $end value :
$myClass->Diff($start);
// Automatically the end is the current timestamp
?>
OK created function for what I need, but still need the right code to complete it:
Code:
$cur_date = date("y/m/d");
$max_date = "12/3/8" // from get_max_date();
$dif_date = e_days($max_date, $cur_date);
echo "Report Days=> $dif_date <br>";
function e_days($start,$end) {
/************************************************************************/
/* Purpose: To get the elapsed days of date diff as integer. */
/************************************************************************/
$s_date = date_create($start);
$e_date = date_create($end);
$r_date = date_diff($e_date, $s_date);
return $r_date;
} // end function
Looked at the "idate" function, but does not have option for elapsed days.Right now getting error:
Quote:
Catchable fatal error: Object of class DateInterval could not be converted to string in /var/www/intranet/wordpress/schedule_test/functions/functions.php on line 49
I've tried the DateInterval options and get the error I posted at:
DateInterval is an object. Most objects in the default PHP api do not override the __toString magic method, so they cannot be directly printed. You can catch it using a try/catch, but that is not typically what you are wanting to do.
You need to print it out by formatting it:
PHP Code:
print $r_date->format('Report Days => %a days');
I personally use OO wheneer I can.
PHP Code:
$dtStart = new DateTime(); $dtEnd = new DateTime('12/3/8'); // This is fine as long as it means December 3, 2008. Otherwise, you need to use mktime to force it manually since strtotime only works in this format with m/d/yy[yy]
$interval = $dtEnd->diff($dtStart); print $interval->format('%a days different');
You are not missing anything; datetime is a part of the core in 5.2+
The error message indicates you're provided date is invalid. If you use / separated date, it requires that you use it in the format of m/d/yy[yy], and is the standard for gnu date time formats. There is no 20th month, and that is what it is complaining about.
Got around some of the errors with type cating in the following function:
Code:
function get_date($mydate) {
/************************************************************************/
/* Purpose: To get only the date string from the passed date/string. */
/************************************************************************/
$datstr = (float) $mydate;
$new_date = date('y/m/d', $datstr);
return (string) $new_date;
} // end function
Now working on the errors from the date math calls.
$cur_date = date("y/m/d");
$max_date = get_max_date();
$dif_date = e_days($max_date, $cur_date);
function e_days($end,$start) {
/************************************************************************/
/* Purpose: To get the elapsed days of date diff as integer. */
/************************************************************************/
$dts = date('m/d/Y',(float)$start);
$dte = date('m/d/Y',(float)$end);
echo "ST=> $start DT=> $dts SE=> $end DE=> $dte <br>";
$dtS = new DateTime($dts);
$dtE = new DateTime($dte);
$int = $dtE->diff($dtS);
print $int->format('%a days different');
//$r_date = date_diff($dte, $dts);
return (integer) $interval;
} // end function
But conversion of date from format 'y/m/d' to 'm/d/Y' completely changes the values of the dates. I saw a writeup on this, but do not know the fix. Right now going to change the statements passing date to the function so will be in '/m/d/Y' format to eliminate the errors.
If you have fix for the date "format" value change, then please submit.
The above definitely won't work as intended.
date creates a string in the format specified using the value of the integer timestamp. Attempting to cast a string to a numeric type is successful until it cannot find a valid replacement. $dts above will contain the value 01/01/1970 indicating 11 seconds since unix epoch.
If you use y/m/d format consistently with your data, you can construct this using createFromFormat on the datetime class available from 5.3+.
function e_days($end,$start) {
/************************************************************************/
/* Purpose: To get the elapsed days of date diff as integer. */
/************************************************************************/
$dtS = new DateTime($start);
$dtE = new DateTime($end);
$int = $dtE->diff($dtS);
$ret = (integer) $int->format('%a');
return $ret;
} // end function
If its already in the format of using m/d/y, any strtotime method will work. m/d/yy[yy] is a standard in gnu time format.
This isn't an issue with PHP now. This is an issue with your data being stored in multiple formats. Previous issues you had was using y/m/d and d/m/y formats. Using different types of formats provides no consistent way to verify. I can validate if 20/01/01 is invalid (which it is), but I cannot validate that 02/01/01 is valid or invalid. Is that supposed to be january 2, 2001, or february 1, 2001? Stick with either a unixtimestamp or a database driven date format instead, which will match the formats usable by the gnu format.
Follow this page under the calendar items to see what formats are considered legal by methods like strtotime: http://www.gnu.org/software/tar/manu...t-formats.html
PHP adheres to most of the rules above. Some items do not work (like fortnight), but these can get around all of these by using +2 week for example.
Hi all,
works great until php 5.3, i got these errors:
Warning: date_diff() expects parameter 1 to be DateTime, string given in /mnt
it was working until php 5.3 cames out. Any ideas how to get it back to work with php 5.3?
The problem here is your custom signature doesn't adhere to the signature used by the built in function. The built in signature is DateInterval date_diff(DateTime, DateTime, boolean);, but the custom one is int date_diff(String, String, String);. Therefore, since you've moved to 5.3 and the date_diff function became available, you can no longer use any signatures of the custom function. Everywhere you have used date_diff will need to be replaced completely to work with the datetime::diff, or you can rename the custom function and rename all the calls to it in order to continue using your custom function.