Solved

how to calculate 2 weeks from now

Posted on 2008-06-26
5
432 Views
Last Modified: 2013-12-13
I have dates in this format
6-30-2008
12-31-2008
12-1-2008
12-1-2008
5-30-2008
6-30-2008
7-7-2008
2-26-2009
12-1-2008
2-27-2008
2-11-2009
2-11-2008
2-11-2009
2-11-2008
5-1-2008
5-1-2009
5-30-2008
 and I want to calculate which date from above falls within two weeks from today. How would I go calculate that
Thanks
0
Comment
Question by:syedasimmeesaq
  • 4
5 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 21878429
php is one of the few languages that makes this real easy.

$futuredate = strtotime($originaldate . " +2 weeks")

0
 
LVL 31

Expert Comment

by:Frosty555
ID: 21878502
oh and I guess if you want it back in the same string format that you had the original date in, as opposed to a numeric php date type:

$futuredate = date("d-m-Y", strtotime($originaldate . " +2 weeks"));
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 21878540
Finally, if you want to calculate what the date is two weeks from NOW:

$futuredate = strtotime(date("d-m-Y") . " +2 weeks");

For each date in your list, do
if( strtotime($originaldate) == $futuredate )  {
    echo "that's the one.";
}
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 21879117
I did this
<?
$sqlmain = mysql_query("select * from table");
$futuredate = strtotime(date("d-m-Y") . " +2 weeks");
$row = mysql_fetch_array($sqlmain);
$numrows=mysql_num_rows($sqlmain);
?>
<table width="90%" border="2">
 <tr>
 <td>Start Date</td>
    <td>End Date</td>
    <td>Future Date </td>
    <td>Matched</td>
   
    </tr>
<?
$i = 0;
  while($i<$numrows){

$startdate = mysql_result($sqlmain, $i, "bstart_district");
$enddate = mysql_result($sqlmain, $i, "bend_distroct");

 
?>  
  <tr>
    <td><?php echo $startdate; ?></td>
    <td><?php echo $enddate; ?></td>
    <td><?php echo $futuredate; ?></td>
    <td>
   <? if( strtotime($startdate) == $futuredate )  {
    echo "that's the one.";
}
 ?>  

It puts the date in some other format like this

Start Date  End Date          Future Date          Matched
9-25-2007  9-26-2007     1215666000  
9-12-2007  9-12-2007     1215666000  
9-25-2007  9-26-2007     1215666000


thanks
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 21880743
Yes, the format is an internal PHP date format - it represents the number of seconds since jan 1900 or something like that. I don't quite remember.

It is best to deal with this format as much as possible. When extracting data from MySql, the date will come as a string. You use strtotime() to convert the string to a PHP date number type which can be used in PHP's wide range of date functions. Then you use date() to reconvert it back to a string for display purposes.

$formatteddate = date("d-m-Y", $futuredate);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question