Solved

ENTRY AGE IN A REPEATING REGION, DREAMWEAVER

Posted on 2013-12-17
10
410 Views
Last Modified: 2014-02-04
I am working in a php/mysql -dreamweaver environment.  I want to calculate the age in days of each entry.  I have the date of the entry saved in the mysql table.  

Date format  is  " mm/dd/yyyy "

How do I add an age field to show how old the entry is in days?
0
Comment
Question by:jws2bay
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39725013
Here's my own comment. Getting age by years is pretty easy. Getting it by days is a little more complex, since there can be more than 365 days in a year (for leap years). Here's a little PHP class I just whipped up that should give you the age in either years or total # of days:

<?php
date_default_timezone_set("UTC");

class Birthday
{
	// DOB parts
	public  $originalFormat;
	private $tsDOB;	// Timestamp
	private $mm;		// DOB month with leading zeros
	private $dd;		// DOB day with leading zeros
	private $yyyy;	// DOB year
	
	// Some calculated values
	public $hasHadBirthdayThisYear;
	public $lastBirthday;
	private $tsLastBirthday;
	
	// Our constructor
	public function __construct($birthday)
	{
		// Store original value
		$this->originalFormat = $birthday;
		
		// Get the parts necessary for DOB calculate
		$this->tsDOB = strtotime($birthday);
		list($this->mm,$this->dd,$this->yyyy) = explode("/", date("m/d/Y",$this->tsDOB));
		
		// Has the birthday passed this year?
		$this->hasHadBirthdayThisYear = (date("md") >= ($this->mm.$this->dd));
		
		// Calculate last birthday
		$this->lastBirthday = $this->mm."/".$this->dd."/".(date("Y") - ($this->hasHadBirthdayThisYear ? 0 : 1));
		$this->tsLastBirthday = strtotime($this->lastBirthday);
	}
	
	public function GetAgeInYears()
	{
		$ageInYears = (date("Y")-$this->yyyy) + ($this->hasHadBirthdayThisYear ? 1 : 0);
		return $ageInYears;
	}
	
	public function GetAgeInDays()
	{
		// Calculate age in years
		$ageInYears = $this->GetAgeInYears();
		
		// Now convert to days
		$daysInAYear = 365.242;
		$ageInDays = $ageInYears * $daysInAYear; // Total of 365.242 days in a year, so we'll end up with a fraction most likely
		
		// Increase by # of days since last birthday
		$daysSinceLastBirthday = ((time() - $this->tsLastBirthday) / 86400);
		$ageInDays += $daysSinceLastBirthday;
		
		// Round down
		$ageInDays = floor($ageInDays);
		
		return $ageInDays;
	}
}

$Birthday = new Birthday("04/22/1980");
echo "Age is: ".$Birthday->GetAgeInYears() . " years or " . $Birthday->GetAgeInDays() . " days\n";
echo "\n";

$Birthday = new Birthday("03/21/1983");
echo "Age is: ".$Birthday->GetAgeInYears() . " years or " . $Birthday->GetAgeInDays() . " days\n";
echo "\n";

$Birthday = new Birthday("12/18/2012");
echo "Age is: ".$Birthday->GetAgeInYears() . " years or " . $Birthday->GetAgeInDays() . " days\n";
echo "\n";
?>

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39725021
I would store only the birthdate in the db.  Then calculate the age in days using js/jquery

http://jsbin.com/UXIMeVA/1/edit?html,js,output

<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>padas</title>
</head>
<body>
  <input id="date" name="date">
  <div id="result">Please endter a date</div>
</body>
</html>

Open in new window

$('#date').change(function(){
  var date_enter=$(this).val();
  
  var days = (new Date() - new Date(date_enter)) / (1000 * 60 * 60 * 24);
  var  days_round_down=Math.floor(days);
  $('#result').html('You are '+days_round_down+' days old');
  
});

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39725412
Here is some information about how to handle DATETIME values in PHP and MySQL.  Please read it over and post back if you still have questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 

Author Comment

by:jws2bay
ID: 39725480
I have a working jquary function to calculate the difference between two dates dates.

What I  want is to the calculate  and show the age for each item in a repeated region on the page.

 I am using dreamweaver on this page, but don't know how to impliment this function.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39725716
Dreamweaver is not the issue.  You can use dreamweaver or notepad to edit your page.  What matters is the final rendered html.  If you can show us your html code (view source) we can help update your jquery.  Or just provide a link to your page.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39726560
This is computationally less efficient, (not that it matters) but precise to the day.
http://www.laprbass.com/RAY_temp_jws2bay.php
Outputs:
December 1 => 17
Yesterday => 1
Today => 0
Tomorrow => 0
May 12, 1993 => 7525
Sep 15, 1950 => 23105
Mar 24, 1917 => 35333

<?php // RAY_temp_jws2bay.php
error_reporting(E_ALL);

date_default_timezone_set("UTC");

function age_in_days($birthday, $currently='TODAY')
{
    $tsb = strtotime($birthday);
    $tsc = strtotime($currently);
    if ($tsb)
    {
        if ($tsc)
        {
            $alpha = date('Y-m-d', $tsb);
            $omega = date('Y-m-d', $tsc);
            $kount = 0;
            while ($alpha < $omega)
            {
                $kount++;
                $alpha = date('Y-m-d', strtotime("$alpha + 1 DAY"));
            }
            return $kount;
        }
        else
        {
            // CURRENTLY IS WRONG
            return FALSE;
        }
    }
    else
    {
        // BIRTHDAY IS WRONG
        return FALSE;
    }
}

// TEST CASES
$arr = array
( 'December 1'
, 'Yesterday'
, 'Today'
, 'Tomorrow'
, 'May 12, 1993'
, 'Sep 15, 1950'
, 'Mar 24, 1917'
)
;
foreach ($arr as $d)
{
    echo PHP_EOL . "<br>$d => ";
    echo age_in_days($d);
}

Open in new window

You probably want to rethink your date format of mm/dd/yyyy.  There is an ISO-8601 standard for computer representations of date/time values, and it exists for a variety of reasons.  One of most important reasons is that the standard disambiguates dates like 03/05/yyyy.  Is that March 5 or May 3?  If you follow the standard, there is no ambiguity.  This is all explained in the article.

Best of luck with your project, ~Ray
0
 

Author Comment

by:jws2bay
ID: 39732129
I got pulled away from this issue.   Shown below is the page code for the repeating section.

The function to calculate the age is called displayDate(<?php echo $row_RFQ['Date']; ?>), and it will display the age at id="AgeII" in the repeating region.

How do you call the function from within the repeating region without an action to attach it?


*******************

<div id="StatusTbl" style="overflow:auto; height:650px"><?php do { ?>
                              <table width="100%" border="0" cellpadding="2">
                                <tr class="style25">
                                  <td width="3"  align="left">|</td>
                                  <td width="25"  align="left" ><a onclick="showDetails(<?php echo $row_RFQ['RFQ_No']; ?>)"><?php echo $row_RFQ['RFQ_No']; ?></a></td>
                                  <td width="2"  align="center">&nbsp;</td>
                                  <td width="30"  align="center"><?php echo $row_RFQ['Status']; ?></td>
                                  <td width="2"  align="center">&nbsp;</td>
                                  <td width="50"  align="center"><?php echo $row_RFQ['Date']; ?></td>
                                  <td width="2"  align="center" id="AgeII">&nbsp;</td>
                                  <td  align="center"><?php echo $row_RFQ['Rep_name']; ?></td>
                                  <td width="2"  align="center">&nbsp;</td>
                                  <td width="150" align="center"><?php echo $row_RFQ['Name']; ?></td>
                                  <td width="2"  align="center">&nbsp;</td>
                                  <td width="120"  align="left"><?php echo $row_RFQ['Phone']; ?></td>
                                  <td  width="2" align="center">&nbsp;</td>
                                  <td  width="300" align="left"><?php echo $row_RFQ['Message']; ?></td>
                                  <td  width="2" align="center">|</td>
                                  </tr>
                                <tr class="style25">
                                  <td colspan="15"  align="center"><img src="Assets/dividerA.gif" width="100%" height="3"></td>
                                  </tr>
                                </table>
                              <?php } while ($row_RFQ = mysql_fetch_assoc($RFQ)); ?></div>
0
 

Author Closing Comment

by:jws2bay
ID: 39834280
I apologize for letting this question hang.  Had a little crisis here.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39834318
What was the reason for giving a marked-down grade?  Please see the grading guidelines and give us the explanation, thanks.
http://support.experts-exchange.com/customer/portal/articles/481419
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Online file editor, manager 6 59
php ssh2_scp_send 1 46
.php tree directory? 5 55
Download a website to hdd 2 49
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
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.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now