Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting number of days

Status
Not open for further replies.

storm197

MIS
Oct 9, 2002
55
CA
Hello there,

I have a question,

I work on a system with a MSSQL database for DB server and PHP.

The MSSQL server date format is the following :
Feb 3 2005 12:00AM

I have two date fields and I just want to calculate the number of days between those two dates.

I tried with the following code :
$bla=strtotime('$date_created')-strtotime('$date_resolved')) /86400;

But it always gives me a 0 value.

Anyone can help me on this ?
 
Is $date_created later than $date_resolved? If not, you're going to get a negative value from your addition. That wouldn't give you a zero, though -- just a negative answer.

Also, in your example your parentheses do not balance. But you do not report a parse error, so I'll interpret that as a typo.

My test script:

Code:
<?php
print date ('Y-m-d H:i:s', strtotime('Feb 3 2005 12:03AM'));
?>

returns "2005-02-03 00:03:00", which tells me that strtotime() can handle a date of that format.

And this script:

Code:
<?php
print (strtotime('Feb 3 2005 12:03AM') - strtotime('Jan 20 2005 12:03AM'))/86400;
?>

returns "14", so I don't see a flaw in your methodology. Have you verified the values are getting into your variables?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ok, I tried with your example and everything works fine if I put the dates mannually in the code.

But if I try to use the variables, it just doesn't work. Here is my code :

$nb_days =(strtotime('$date_resolved') - strtotime('$date_created'))/86400;

Both strtotime return -1. I've enter manually the real dates of these variables, and it works fine.

I've alse tried with " " and If give me the following error :function.strtotime]: Called with an empty time parameter.
 
You are using single quotes where you shouldn't be:
Code:
$nb_days =(strtotime('$date_resolved') - strtotime('$date_created'))/86400;
should be written:
Code:
$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;
The single quotes around the variable names are turning them into strings which strtotime can't understand.

Ken
 
Yeah, I thought about this one. But when I remove the Quotes, if gives me this error :

Warning: strtotime() [function.strtotime]: Called with an empty time parameter. in c:\Inetpub\ on line 247

If I just echo my variable $date_created and $date_resolved, they seems to be ok.

$date_resolved = Jan 18 2005 4:20PM
$date_created = Jan 31 2005 3:30PM
 
Ken's right. You must NOT have those quotes, as information inside of singlequotes is not interpolated. In short, you're passing strtotime() the literal strings '$date_resolved', not a reference to the value of the variable $date_resolved.


Where are the values in $date_resolved and $date_created coming from? Could this be a problem with register_globals?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
The variables come from a MSSQL query which is done just before the function.

For the query, I do the usual code :

While ($ligne_students_quitted = mssql_fetch_array($result_students_quitted))
{
extract ($ligne_students_quitted);
$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;


}



Register Global is set to ON, i'm on a test environment.
 
If you change:

$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;

to read:

print "'" . $date_resolved . "'\t'" . $date_created . "'";
$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;


Do you see the two dates?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
This code works fine for me:
Code:
<?
$date_resolved = "Jan 31 2005 4:20PM";
$dr = strtotime($date_resolved);
$date_created = "Jan 18 2005 3:30PM";
$dc = strtotime($date_created);
$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;
echo '$date_resolved: ' . $date_resolved . "\n";
echo 'strtotime($date_resolved): ' . $dr . "\n";
echo '$date_created: ' . $date_created . "\n";
echo 'strtotime($date_created): ' . $dc . "\n";
echo '$nb_days: ' . $nb_days."\n";
?>
Giving the results:
Code:
$ php -q strtotime.php
$date_resolved: Jan 31 2005 4:20PM
strtotime($date_resolved): 1107206400
$date_created: Jan 18 2005 3:30PM
strtotime($date_created): 1106080200
$nb_days: 13.034722222222

$ php -v
PHP 4.3.10

Echo your variables just before your calculation.

BTW, development or production, register_globals should be set to OFF.

If it's set to ON, write your code as if it's set to OFF.

Ken
 
Really really strange here....

It works fine if I put the following code :

echo "<td bgcolor=\"$row_color\" align=\"center\" class=\"texte\">$date_created<br>$date_resolved";

$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;

echo "<br>".$nb_days;

echo "</td>";


Doesn't work If I put it like this :

$nb_days =(strtotime($date_resolved) - strtotime($date_created))/86400;

echo "<td bgcolor=\"$row_color\" align=\"center\" class=\"texte\">$date_created<br>$date_resolved";

echo "<br>".$nb_days;

echo "</td>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top