Jump to content

PHP age in year month day

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
usmanzm

usmanzm

    Newbie

  • Members
  • Pip
  • 5 posts
function get_umur($var){
  $tglsekarang = date("Y-m-d");
  $query = "SELECT datediff('$tglsekarang', '$var') as selisih";
  $hasil = mysql_query($query);
  $data = mysql_fetch_array($hasil);

  $tahun = floor($data['selisih']/365);
  $bulan = floor(($data['selisih'] - ($tahun * 365))/30);
  $hari = $data['selisih'] - $bulan * 30 - $tahun * 365;
  return $tahun."th ".$bulan."bln ".$hari."hr";
}

Edited by Jordan, 01 October 2008 - 07:10 AM.


#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Just a few tips mate.

When you are posting code make sure you wrap it in
CODE GOES HERE
or
CODE GOES HERE
. It makes it more understandable.

This is a very basic script and thank-you for posting. But another tip to the actual code, when you are naming a variable it's better to keep short but understandable names for future references. If you wish you can add comments.

Since I am starting to post some of my most basic code here I am going to add comments for newbies to understand the code.

Other than that, I wanted to make a script like this but didn't never felt like it. So thank-you :)

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Again, I've added code tags. Please use these in the future. I'd also like to point out you are using a SQL Query and Database Connection to do a very simple task:

$query = "SELECT datediff('$tglsekarang', '$var') as selisih";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);

That is a lot of overhead (and pointless) for no good reason. Doing the date difference calculation via code is much faster and efficient. You can find some quick functions on the net such as here: Calculating Difference Between Two Dates Using PHP

An easy method that I have used in the path is to convert both dates to a Unix Timestamp and then subtract them. Once calculated convert it back. You could also use a date class like the PEAR::Date library (pick the 1.5 Alpha release) which has a Date_Calc class for calculations.

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

Jordan said:

Doing the date difference calculation via code is much faster and efficient.
Both of you are doing it in code; it's just that you're getting it calculated on the MySQL query, whereas the less efficient one is doing it manually in PHP.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
I agree with what Jordan said, change it into a unix timestamp. Subtract them and convert back into new time.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!