ab5ni
Posts: 177
|
| Posted: 04/12/2011, 7:52 AM |
|
Hi Folks,
I have two dates: today's date and a date set previously. I'd like to be able to calculate the number of days between these two dates and display the difference in a text label. Searched the forums and didn't find anything. I'd like to accomplish this server side in php.
TIA,
Randy
_________________
Randall Jouett
Amateur Radio: AB5NI
I eat spaghetti code out of a bit-bucket while sitting at a hash table! And yes, I paid for the meal in cache!
|
 |
 |
damian
Posts: 838
|
| Posted: 04/12/2011, 8:18 AM |
|
search on strtotime...
http://forums.codecharge.com/search.php?s_keyword=date+...[]=25&s_period=
http://www.google.com.au/search?q=strtotime+php
_________________
if you found this post useful take the time to help someone else.... :)
|
 |
 |
ab5ni
Posts: 177
|
| Posted: 04/19/2011, 8:53 AM |
|
Thanks for a the info. I looked at it all, and even tried most of the solutions, but I can't seem to get any of them to work using the CCS methods described. For instance:
// This is all being displayed on a list grid, btw.
/* Label of type "Date" set from datepicker on maint page..everything set correctly, using datetime
format of MySQL.
*/
$date1 = $Component->date_entered1->GetValue();
if($date1 == NULL)
$Component->text_label1->SetValue("date1 is NULL");
This code displays "date1 is NULL" every time. I have checked all the DB-formats, etc. What gives?
Note: date_entered1 is being displayed correctly as a proper date.
Randy
_________________
Randall Jouett
Amateur Radio: AB5NI
I eat spaghetti code out of a bit-bucket while sitting at a hash table! And yes, I paid for the meal in cache!
|
 |
 |
Waspman
Posts: 948
|
| Posted: 04/20/2011, 1:04 AM |
|
heve you seen this...
If you've got the two dates on the page visible or not...
jjrjrr1 (?) sorted this when he explained how CCS converts dates to an array before presentation.
go here;
http://forums.yessoftware.com/posts.php?post_id=97052
_________________
http://www.waspmedia.co.uk |
 |
 |
ab5ni
Posts: 177
|
| Posted: 04/26/2011, 8:25 AM |
|
Well, I got it fixed, to some extent, but I'm still having problems. Here's the code:
/* Note: This function is busted, somewhat, in that it doesn't take leap years into account,
and every month is considered to have 30 days, etc. PHP version 5.3 fixes all this, with
"num of days different" being an added member function to the Date class.
In other words, this is test code to see if things are working correctly.
*/
// This code is run via "Before Show" event.
Function DateDiff($today, $compare_date)
{
$tod = strtotime($today); // Can use strtotime("now") here, of course, but picking it up from textbox/datepicker..
$test_date = strtotime($compare_date);
$diff = $tod - $test_date;
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
return($days);
}
Function OneDayCheck($date_diff)
{
if($date_diff == 1)
return("day");
else
return("days");
}
// Yeah, I know. What if there are zero days different :D. This is test code :p
$db = new clsDBConnection1();
$todays_date = CCDLookUp("todays_date","HSE","hse_id=" . $Container->hse_id->GetValue(), $db);
$record_only = CCDLookUp("record_only","HSE","hse_id=" . $Container->hse_id->GetValue(), $db);
$days_diff = DateDiff($todays_date, $record_only);
$num_days_diff = OneDayCheck($days_diff);
$Container->record_only_text->SetValue("(" . $days_diff . " " . $num_days_diff . ")");
$first_aid = CCDLookUp("first_aid","HSE","hse_id=" . $Container->hse_id->GetValue(), $db);
$days_diff = DateDiff($todays_date, $first_aid);
$num_days_diff = OneDayCheck($days_diff);
$Container->first_aid_text->SetValue("(" . $days_diff . " " . $num_days_diff . ")");
$db->close();
/*
There is going to be a ton of dates for comparison in the record, so I'll eventually roll it up into
on big SQL statement that reads them all in a single shot, rather than calling CCDLookUp() over
and over. WAY too many queries and much slower.
*/
This code works, yet it doesn't. The number of days different will only display correctly when I go to the maintainance page, make no changes to the record, and hit submit. Otherwise, when the list page loads, all the number of days different are all zero, as if it never really loaded a record, yet all the dates are being displayed. Any suggestions on fixing this, please?
TIA
Randy
_________________
Randall Jouett
Amateur Radio: AB5NI
I eat spaghetti code out of a bit-bucket while sitting at a hash table! And yes, I paid for the meal in cache!
|
 |
 |
Waspman
Posts: 948
|
| Posted: 04/28/2011, 6:55 AM |
|
Yeah look at jjrjrr1's post!
I wouldn't try to run a function in the before show event. Use what John says it's a lot easier.
_________________
http://www.waspmedia.co.uk |
 |
 |
ab5ni
Posts: 177
|
| Posted: 04/28/2011, 10:46 AM |
|
Howdy, Wasp, and thanks for the reply...
Well, I tried what he said and it didn't work :/. $date[0] and $date[1] are coming up null, unfortunately.
Any other thoughts?
Randy
_________________
Randall Jouett
Amateur Radio: AB5NI
I eat spaghetti code out of a bit-bucket while sitting at a hash table! And yes, I paid for the meal in cache!
|
 |
 |
datadoit
|
| Posted: 04/28/2011, 11:16 AM |
|
Well, there may never actually be a situation where $date == NULL. If
you're letting CodeCharge format your date by specifying the field as a
date field, then it builds the field value into an array. That's what
the post is referring to, array values (date[0], etc.). The value may
actually look something like '0000-00-00 00:00:00'.
I go back to what I preach constantly - debug using echo. Echo to the
screen in the field's OnValidate event the value.
echo "The Date: " . $Component->GetValue(); exit;
There's even an action 'Print Text' that also does this. Use it, love
it, be happy.
Also, since you're able to set the value of the field to "date1 is
NULL", that tells me that you don't really have the field defined as
date, but rather as text. If it were set to date, then it should throw
a type-mismatch error when CodeCharge attempts to parse 'date1 is NULL'
into a date format.
Finally, checking for 'NULL' will be different with various databases
and scripting languages. Just check for:
if (!$Component->GetValue())
|
|
|
 |
ab5ni
Posts: 177
|
| Posted: 04/28/2011, 9:58 PM |
|
Hi, Data, and thanks for the reply.
Quote :
Well, there may never actually be a situation where $date == NULL. If
you're letting CodeCharge format your date by specifying the field as a
date field, then it builds the field value into an array. That's what
the post is referring to, array values (date[0], etc.). The value may
actually look something like '0000-00-00 00:00:00'.
But that's what I've been saying here. It is NULL 
Maybe something is busted on my end, but all I can say is that when I do this:
$date = $Component->todays_date->GetValue();
if($date[0] == NULL)
$Component->text_label->SetValue("Yep...it is NULL");
else
$Component->text_label->SetValue("Non-NULL Value");
I get NULL -- no and's, if's, or but's about it. Now, there could be a server problem or error showing up, and I'll definitely look into this, for sure.
Quote :
I go back to what I preach constantly - debug using echo. Echo to the
screen in the field's OnValidate event the value.
echo "The Date: " . $Component->GetValue(); exit;
There's even an action 'Print Text' that also does this. Use it, love
it, be happy.
Well, I'm printing debug info. I'm just using a text label on the page to do so .
I tried the PHP standard echo on the page at various locations, and could never
get it to work, and I'm way too busy to mentally parse the code to find out why.
Thanks much for that info, though.
Quote :
Also, since you're able to set the value of the field to "date1 is
NULL", that tells me that you don't really have the field defined as
date, but rather as text. If it were set to date, then it should throw
a type-mismatch error when CodeCharge attempts to parse 'date1 is NULL'
into a date format.
I checked and rechecked this, Data. $Component->todays_date is set correctly, and
it is of type date. The date it is being compared to is also of type date, and both of
these are being sent to strtotime(), with the database format being yyyy-mm-dd, and
CodeCharge format being exactly the same. I subtract these two dates,
and then I set the difference/delta to a text label on the page. This code works, but it
displays "0 days" on initial loading of the page. It also displays "0 days" if I reload the page.
OTOH, if I edit the record via a CodeCharge-generated maintainance page, make no changes,
and hit submit, it redirects to the list page (as it should) and then displays everything correctly --
deltas and all. Go figure 
Quote :
Finally, checking for 'NULL' will be different with various databases
and scripting languages. Just check for:
if (!$Component->GetValue())
So, what u are saying here is this: !$Component->todays_date->GetValue() is not the same as
if($Component->todays_date->GetValue() == NULL) ??
BTW, I've been doing this thing we call programming (and designing computers, too) for 30 years.
Please feel free to give a more verbose explanation of the inner workings if you find it necessary, Data.
(Old assembly hack and compiler-design freak, plus a lot of analog and digital RF work )
Best Regards,
Randy
_________________
Randall Jouett
Amateur Radio: AB5NI
I eat spaghetti code out of a bit-bucket while sitting at a hash table! And yes, I paid for the meal in cache!
|
 |
 |
datadoit
|
| Posted: 04/29/2011, 5:53 AM |
|
So let me repeat:
"Echo to the screen in the field's OnValidate event the value.
echo "The Date: " . $Component->GetValue(); exit;"
Did you do this? and what did you see or not see? You 'should' see:
"The Date: Array"
You can see the contents of that array using the 'print_r' command. If
you don't see anything, then obviously the problem is how or where
you're assigning the field's value.
Don't use $Component->todays_date->GetValue(). Use either
$Container->todays_date->GetValue() or $Component->GetValue() if you're
using a field server-side event.
|
|
|
 |
ab5ni
Posts: 177
|
| Posted: 04/29/2011, 8:59 AM |
|
Hi again, Data, and thanks for the reply.
I'll give it a shot and see what gives. BTW, let's say that it does display an array. (It probably will, because the dates are showing up on the list page, but the text label displaying the difference isn't on page load -- just the numerical value zero.) So, given that situation, I'm still stuck with the same problem. Why do I have to edit via the maintainance page, hit submit, and everything displays correctly? Also, I'm only displaying one record per page, and I'm wondering if that could have something to do with this situation? Will change it and see.
Regards,
Randy
_________________
Randall Jouett
Amateur Radio: AB5NI
I eat spaghetti code out of a bit-bucket while sitting at a hash table! And yes, I paid for the meal in cache!
|
 |
 |
jjrjr2
Posts: 131
|
| Posted: 04/29/2011, 5:12 PM |
|
Hi
$date = $Component->todays_date->GetValue();
if($date[0] == NULL)
$Component->text_label->SetValue("Yep...it is NULL");
else
$Component->text_label->SetValue("Non-NULL Value");
Im that code example it seems U missed the meaning of my post.
If your date component is a CCS date field in the above code fragment $date[0] will equal 0 not null.
As mentioned in the old post the [0] array element contains a unix time stamp (or should).
If a date is in that component than it is the timestamp (a date represented in seconds from the begining of the Unix epoch)
Believe me. The info I posted was well tested and if U are having trouble with it let me know..
U should be able to do what U R trying to do in just a few lines of code..
Try some debugging as Datadoit suggests. echo out some values till U understand what is happening.
Hope that helps...
John
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
Real Web Development At: http://RealWebDevelopment.us |
 |
 |
|