This topic is locked
[SOLVED]

 date calculation

3/20/2012 11:55:04 AM
PHPRunner General questions
swami author

Hello how can i get date calculation .. as from the date enter in past to now in number format to new field.

thanks for help .

C
cgphp 3/20/2012

Do you want to show on a field in the list page the days till today?

swami author 3/20/2012



Do you want to show on a field in the list page the days till today?



yes in the listpage !!!

C
cgphp 3/20/2012

Set the field as Custom in the "View as" setting and enter the following code:

$value = floor((time() - strtotime($value)) / 86400);
swami author 3/20/2012



Set the field as Custom in the "View as" setting and enter the following code:

$value = floor((time() - strtotime($value)) / 86400);



thanks a lot Cristian for your help .. but i get the number like 15419 on the set field where date posted just 1/3/2012 actually it should be 19 days ??

C
cgphp 3/20/2012

It looks like your date field is not a date type. How do you save dates in the db? As datetime?

swami author 3/20/2012



It looks like your date field is not a date type. How do you save dates in the db? As datetime?



yes it is as datetime

C
cgphp 3/20/2012

Have you set the "Custom" setting for the right field?

swami author 3/20/2012



Have you set the "Custom" setting for the right field?


here what i did , may be i mistake please have a look
$value = $data["due_date"];

$value1 = $data["date"];

$value = floor((time() - strtotime($value1)) / 86400);

C
cgphp 3/20/2012

What is the name of the field where you want to see the number of the days?

swami author 3/20/2012



What is the name of the field where you want to see the number of the days?


due_date

C
cgphp 3/20/2012

Set due_date as Custom and enter the following code:

$value = floor((time() - strtotime($data['date'])) / 86400);


Is the date field a date type?

swami author 3/20/2012



Set due_date as Custom and enter the following code:

$value = floor((time() - strtotime($data['date'])) / 86400);


Is the date field a date type?


thanks a lot your time Christian, as u ask the date is set as DATETIME shall i put just as DATE ?

C
cgphp 3/20/2012

No, datetime is correct.

swami author 3/20/2012



No, datetime is correct.


sorry to say i get the same number 15419 !!! i change to DATE .. but still got the same value.. !!

C
cgphp 3/20/2012

Which format is used for the date in the database?
From the strtotime function docs:



Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates

swami author 3/20/2012



Which format is used for the date in the database?
From the strtotime function docs:


DATETIME format was 20/03/2012 because i am living in europe. and even now i change the format of date as (YYYY-MM-DD) still it give the same result .. this is strange... !!!!

C
cgphp 3/20/2012

Coul you post a screenshot of your table structure and data in phpmyadmin?

swami author 3/20/2012



Coul you post a screenshot of your table structure and data in phpmyadmin?


here we go .. i put together .. thanks for all...

http://www.screencast.com/t/2w5O4GtTeqB9

C
cgphp 3/20/2012

Please, post a screenshot of phpmyadmin of the data not only the structure.

swami author 3/21/2012



Please, post a screenshot of phpmyadmin of the data not only the structure.


OK here is the screenshot of date. thanks a lot for ur continuously support and concern... hope we will find the solution.

http://www.screencast.com/t/2nwNHCBj

C
cgphp 3/21/2012

Try this version:

$value = intval((time() - strtotime($data['date'])) / 86400);
swami author 3/21/2012



Try this version:

$value = intval((time() - strtotime($data['date'])) / 86400);




Cristian sorry to say but result is same as before <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=65091&image=1&table=forumreplies' class='bbc_emoticon' alt=':o' /> .. no change ..same as before the value is 15420
this is what is inside custom of dueDate .. is this right or has some mistake ?

$value = $data["due_date"];

$value = intval((time() - strtotime($data['date'])) / 86400);


may be there is bug in version 6 or what ??

C
cgphp 3/21/2012

You don't need the first line. The only code needed is the following:

$value = intval((time() - strtotime($data['date'])) / 86400);
swami author 3/21/2012



You don't need the first line. The only code needed is the following:

$value = intval((time() - strtotime($data['date'])) / 86400);



got the same result ... here is the screenshot http://www.screencast.com/t/JB6BaSs3DwA
how about if i add another date eg. mod_date so we can find the calculation between 2 dates .. what could be the calculation code .

C
cgphp 3/21/2012

Ok, I have found the bug. The date field name is date_time not date. Try this version:

$value = intval((time() - strtotime($data['date_time'])) / 86400);
swami author 3/21/2012



Ok, I have found the bug. The date field name is date_time not date. Try this version:

$value = intval((time() - strtotime($data['date_time'])) / 86400);



YES YES .. now works !! Thanks a lot Cristian for your time and help !!!!!!!!! <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=65098&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />