Forums: Calculating dates in fhe future - Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Calculating dates in fhe future

#1 User is online   ficc 

  • Member
  • PipPip
  • Group: Members
  • Posts: 22
  • Joined: 25-February 09
  • Gender:Male
  • Location:Lisbon
  • Interests:Too many to list here.. :o)

Posted 12 October 2009 - 12:53 PM

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.
0

#2 User is offline   tedwilder 

  • Member
  • PipPip
  • Group: Members
  • Posts: 12
  • Joined: 01-December 09

Posted 04 December 2009 - 11:29 AM

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"])+(60*60*24*30*6));

It does all you did but in one line ! :)
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
0

#3 User is online   ficc 

  • Member
  • PipPip
  • Group: Members
  • Posts: 22
  • Joined: 25-February 09
  • Gender:Male
  • Location:Lisbon
  • Interests:Too many to list here.. :o)

Posted 14 January 2010 - 05:11 PM

View Posttedwilder, on 04 December 2009 - 11:29 AM, said:

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"])+(60*60*24*30*6));

It does all you did but in one line ! :)
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
0

#4 User is offline   wildwally 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 71
  • Joined: 01-June 07

Posted 02 March 2010 - 04:42 PM

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.
0

#5 User is online   ficc 

  • Member
  • PipPip
  • Group: Members
  • Posts: 22
  • Joined: 25-February 09
  • Gender:Male
  • Location:Lisbon
  • Interests:Too many to list here.. :o)

Posted 07 April 2010 - 01:09 PM

View Postwildwally, on 02 March 2010 - 04:42 PM, said:

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;

0

#6 User is offline   wildwally 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 71
  • Joined: 01-June 07

Posted 29 April 2010 - 12:45 PM

View Postficc, on 07 April 2010 - 08:09 AM, said:

View Postwildwally, on 02 March 2010 - 04:42 PM, said:

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;



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.
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users