SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Some Help required about date and Time

    Hi

    Please note that in my HTML form i have 6 text boxes, that is

    Day1, Month1, Year1,
    and
    Day2, Month2, Year2,

    both the sets are used to get the Date of birth from the user.

    Now in PHP i want to do following :

    1 - i want to concatinate the Day,Month,Year to create a date object.
    2 - When both the sets are concatinated to represent 2 different dates, i want to determint the younger of them .
    3 - I want to determine the age in years and in months from both the dates.

    Please guide me in this regards !!

    Zeeshan

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no date object per se in PHP. You can get a Unix timestamp integer like this:

    PHP Code:
    $date mktime(0,0,0,$month,$day,$year); 
    The larger of two such integers will be the younger one. Subtract the smaller number from the larger number to get the difference. Each integer represents a certain number of seconds, so you will have to do some math to convert the difference to months and years.

  3. #3
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok this way how can i get the age of both the persons in years ?
    moreover, how can i get their DOB ? can i do this

    echo date('d-m-Y',$date);

    please guide

  4. #4
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Salam,
    I am not clear about ur post...
    R U recieve the day, month, year from select boxes seperatly.

  5. #5
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Walaykum assallam WarehmatAllah

    I have text boxes for the Day, Month and Year. User put the day, month and year by typing.

    Now i want to convert the THREE textbox values to form a PHP date. And then i want to know the User age in months and years.

    Please guide.

  6. #6
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok 1st of all nice to meet u on sitepoint.
    I will suggest u to make select boxes for day, month, and year.

    Then use php varaibles for to make a date.
    How let me explain...

    day (textbox or select box) from html form
    PHP Code:
    $day=htmlentities($_POST['day']);
    $month=htmlentities($_POST['month']);
    $year=htmlentities($_POST['year']); 
    I use htmlentities, this is when u use textboxes not for select boxes,
    This is for security purpose that a user cannot enter some specail characters.
    like (./><~!).hope u got it.
    If u want to use mysql db then for db use like this.
    PHP Code:
    $day=mysql_real_escape_string($_POST['day']); 
    And so on for month and year.

    After all of that now u have to make date in from above $day,$month,$year.

    make a new variable in php under the above variables:
    PHP Code:
    $date=$year"-"$month"-"$day
    Thats all test it and then reply me either it works or not.

  7. #7
    [Biped] LJK's Avatar
    Join Date
    Jun 2004
    Location
    In My Jammies
    Posts
    761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi -
    I'm a newb. w/ php, so perhaps am off -
    but was wondering if this would work:
    PHP Code:
    $day1=$_POST ['year1'];
    $month1=$_POST ['year1'];
    $year1=$_POST ['year1'];
    $day2=$_POST ['year2'];
    $month2=$_POST ['year2'];
    $year2=$_POST ['year2']; 
    then concat. with something like:
    PHP Code:
    $dob1 $day1.'-';
    $dob1 .= $month1.'-';
    $dob1 .= $year1;
    //repeat as $dob2 for the 2's... 
    And then do your comparison using $dob1 and $dob2.

    Good luck,
    El
    F-Fox 2.0 :: WIN :: el design :: US

  8. #8
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh sorry i forgot :

    Try this:

    PHP Code:
    $date=$year."-".$month."-".$day

  9. #9
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have already done that all.

    But that will concat the TEXTs not converthing them to DATE. I want that to be converted in to date, so that i can perform some other Date operations on it.

  10. #10
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this:
    PHP Code:
    $date=strtotime("04-06-2005"); //example date
    echo date('d',$date)."-".date('m',$date)."-".date('Y',$date); 
    my mobile portal
    ghiris.ro

  11. #11
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    thanks for all the replys. Please note that when the user enter theyr date of birth by filing the DAY / MONTH and YEAR text boxes. Then i want to know when its next Birthday is comming ? is it in next 60 days, is it in next 180 days ? or it is next year ? this is waht i want to achieve.

    Please help.

  12. #12
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there any one who may figure out the solution ?

  13. #13
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Countdown timer
    PHP Code:
    function time_until($event$day$month$year "unset"$min 0$hour 0$sec 0){
        
        
    /**
         * The above prints the time until a given date.  
         * This can calculate to any time of day.
         */
        
        /**
         * If you didn't put a year above ($year = "unset"), assume it's the current year
         */
        
         
    if ($year == "unset")
             
    $year date("Y");
        
         
    $left mktime($sec$min$hour$month$day$year) - mktime();
        
        
    /**
         * Lets assum that this is an annual event such as a Birthday and that 
         * it will happen again next year. This starts the counter over again after 
         * the event takes place.
         */
        
         
    if ($left)
             
    $left mktime($sec$min$hour$month$day$year 1) - mktime();
        
        
    /**
         * If there is no time left on the counter (the day has come) 
         * Change the "My" if you want, but leave the $event for the event name 
         * This is set so it will say: "My Birthday"
         */
        
         
    if ($left == 0)
             return 
    "My $event";
        
         
    $time = array(
            
    "year" => 31449600,
             
    "week" => 604800,
             
    "day" => 86400,
             
    "hour" => 3600,
             
    "minute" => 60,
             
    "second" => 1);
        
         
    $ret_str "There ";
        
         while(list(
    $span$scale) = each($time)){
             
    $chunk floor($left $scale);
             
    $left $left $scale;
            
             if (
    $chunk != 0):
                
                
    /**
                 * if there is more than one day left, have are, for 1 day have is
                 */
                
                 
    $p1 = ($chunk == 1) ? "is" "are";
            
             if (
    $chunk 1)
                 
    $span "${span}s";
            
             
    $sep = ($left == 0) ? " and " ", ";
            
             
    $ret_str .= ((!ereg("[0-9]""$ret_str")) ? $p1 $sep) .
             
    $chunk $span";
            
             endif;
            
             }
        
         return 
    $ret_str " until my ${event}.";
        
        }
        
     
    $birthday="1965-04-05"//my birthday ;-)
     
    $dateexplode('-'$birthday);
     
    /**
     *("Event", day of month, month);
     */
    echo time_until("Birthday"$date[2], $date[1]); 
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •