This topic is locked

Calculating dates in fhe future

10/12/2009 8:53:28 AM
PHPRunner Tips and Tricks
ficcionista author

I've been searching for something like this for quite some time and finally managed to get it to work with PHP Runner.
Let's say we are working on an Service Desk type application.

My main table has (among others) the following fields:
opening_date

closing_date

kpi
The "opening_date" is filled automatically using the "now()" as default value in the visual editor.

The "kpi" is selected with a drop-down menu and is used to define what the closing date should be.
So what I do to determine de closing date is use the code below in the events.

// Before record added

function BeforeAdd(&$values,&$message,$inline)

{



$kpi = $values["kpi"];



if ($kpi == "2.1")

$kpidate = date("Y-m-d H:i:s", time()+7200);

else if ($kpi == "2.2")

$kpidate = date("Y-m-d H:i:s", time()+10800);

else if ($kpi == "3.1")

$kpidate = date("Y-m-d H:i:s", time()+14400);

else if ($kpi == "4.1")

$kpidate = date("Y-m-d H:i:s", time()+96400);

else if ($kpi == "4.2")

$kpidate = date("Y-m-d H:i:s", time()+86400);

else if ($kpi == "4.3")

$kpidate = date("Y-m-d H:i:s", time()+345600);

else if ($kpi == "4.4")

$kpidate = date("Y-m-d H:i:s", time()+2592000);

else if ($kpi == "4.5")

$kpidate = date("Y-m-d H:i:s", time()+15552000);



$values["data_fim"] = $kpidate;

} // function BeforeAdd
// Before record updated

function BeforeEdit(&$values,$where,&$oldvalues,&$keys,&$message,$inline)

{



$kpi = $values["kpi"];

$opendate = $values["data_abertura"];



if ($kpi == "2.1")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 120 minutes"));

else if ($kpi == "2.2")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 180 minutes"));

else if ($kpi == "3.1")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 240 minutes"));

else if ($kpi == "4.1")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 1 days"));

else if ($kpi == "4.2")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 1 days"));

else if ($kpi == "4.3")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 4 days"));

else if ($kpi == "4.4")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 30 days"));

else if ($kpi == "4.5")

$kpidate = date("Y-m-d H:i:s", strtotime("$opendate + 180 days"));



$values["data_fim"] = $kpidate;



} // function BeforeEdit


Hope this is helpful.

T
tedwilder 12/4/2009

_

ficcionista author 1/14/2010



I didnt see this before posting the other day , but the admin just gave me a simple string that works ok for me :
it shows a date in 6 months in the future :
$values["dateexpire"] = date("Y-m-d",strtotime($values["dateemission"])+(606024306));
It does all you did but in one line ! <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=46912&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />

Well it has a down side : sometimes the date given is not 100% accurate : it has a 3 days error from time to time but enough accurate for me.

("datemission" is the date to start counting and result is "dateexpire")

problem is Im from france I dont use 1912-02-25

so then I have to add this to make it right :
$date1=$values["dateexpire"];
$annee=substr($date1, -10, 4);

$mois=substr($date1, -5, 2);

$jour=$date1[strlen($date1)-2].$date1[strlen($date1)-1];
$dateexp=$jour . "/" . $mois . "/" . $annee;
so it gives the right order in $dateexp : 25/02/1912


Yes, i agree it is simpler this way, however it does not fit my needs.
The "end date" is not six months; the "end date" changes according to what value you chose from the "kpi" field. That is why i have all thos IFs and ELSEs

W
wildwally 3/2/2010

Any idea on how to not allow the user to pick a weekend?
Our site has a calandar that looks at a table for lead times and calculates earliest allowed date - but can't figure out how to not allow weekends to be selected.

ficcionista author 4/7/2010



Any idea on how to not allow the user to pick a weekend?
Our site has a calandar that looks at a table for lead times and calculates earliest allowed date - but can't figure out how to not allow weekends to be selected.


I have something similar on one of my apps that fills out a date automatically based on the current date. What it does is filling out the date field with tomorrow's date every time you add one record, except on weekends.

Here's the code:

$tomor = date("D");

if ($tomor == "Fri")

$trw = date("Y-m-d", time()+259200);

elseif ($tomor == "Sat")

$trw = date("Y-m-d", time()+172800);

else

$trw = date("Y-m-d", time()+86400);

What this does is check the week day. If we're on a Friday, Saturday or Sunday it will always add Monday's date. If we're on any other day of the week it will add tomorrow's date.

I think that you could try to make something out of this:

$tomorrow = date("D");

if ($tomorrow == "Sat" || $tomorrow == "Sun")

echo "Sorry no weekends allowed;
W
wildwally 4/29/2010



I have something similar on one of my apps that fills out a date automatically based on the current date. What it does is filling out the date field with tomorrow's date every time you add one record, except on weekends.

Here's the code:

$tomor = date("D");if ($tomor == "Fri")$trw = date("Y-m-d", time()+259200);elseif ($tomor == "Sat")$trw = date("Y-m-d", time()+172800);else$trw = date("Y-m-d", time()+86400);

What this does is check the week day. If we're on a Friday, Saturday or Sunday it will always add Monday's date. If we're on any other day of the week it will add tomorrow's date.

I think that you could try to make something out of this:

$tomorrow = date("D");if ($tomorrow == "Sat" || $tomorrow == "Sun")echo "Sorry no weekends allowed;



Adding to this thought would it be possible to not allow the Sat or Sun be counted as days?
What we have is a lead time calendar - each dept has a specific amount of time it takes to do their task. So the user selects a day from the calendar that has to have the minimum of x days from the lead time table. right now it's counting weekends and allowing weekends to be choosen, which we do not want to happen.
I'll play around with the above suggestion; just wondering if anyone had anything else close to this.

O
oermops 9/18/2011

I don't want to sound dense here but can this be used to calculate multiple fields (if so how)? I have a total of 10 fields that I need to calculate, each with different times for the calculation.
For example:
field1 +2years =field1Expired

field2 +3years =field2Expired

*field3 +1year =field3Expired ...
You get the idea.

ficcionista author 10/25/2011

I guess you could do something like this:



$date1 = date_create($values["field1"]);

$date2 = date_create($values["field2"]);

$date3 = date_create($values["field3"]);
$values["field1expired"] = date_add($date1, date_interval_create_from_date_string('1 year'));

$values["field2expired"] = date_add($date2, date_interval_create_from_date_string('2 year'));

$values["field3expired"] = date_add($date3, date_interval_create_from_date_string('3 year'));