Results 1 to 17 of 17

Thread: Date Diff problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Date Diff problem

    Hello there

    I've used many ways but i can't find the solution,my date is stored in a "date" mySql field.
    I want to display full article in 4 four months time after inserted.

    Code:
    ...
    $art_query=mysql_query($getarticles);
    	$n=0;
    	while ($array=mysql_fetch_array($art_query)) {
    						
    $ids[]=$array["id"];						    					
    $date_published[]=$array["date_created"];						
    ....
    //Results of query are in an array i think this makes it all hard
    $mydate=$date_published[$n];
    
     //$mydate->modify("+4 month");tried this too , srttotime,getdate...
    // print $now;
    
    $now  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y"));
    $pub_date =mktime(0, 0, 0, $mydate[month]+4, $mydate[day], $mydate[year]);
    
    		if ($now > $pub_date){
    			echo "something";
    											
    		}else{
    			echo "something else";
    											
    			}
     }
    $n++
    something is wrong with my syntax in $pub_date i guess because when i echo it displays 1-31-2000 which is not really my date record....

    $now works offcourse


  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Date Diff problem

    What do you get if you echo out $mydate[month], $mydate[day] and $mydate[year] prior to mktime()? What are those values? Further, what is $array["date_created"] when echoed?

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Re: Date Diff problem

    $array["date_created"] echoes the correct dates for every record ;

    echo $mydate[day]; echo $mydate[month] == 2 !!!
    don't know where this 2 comes from

    do you know the correct format for the mkdate i need to write?

    thanks a lot

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date Diff problem

    execute the following, and then let us know what is returned:

    PHP Code:
    <?php print_r($mydate); ?>
    it doesn't sound like $mydate is even an array, so you can't just refer to it like you are ($mydate[day], etc).

  5. #5
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Date Diff problem

    I think you may be misunderstanding the concept of mysql_fetch_array() in a while loop. When you use it like you're using it, each loop is a new row of the record set that was returned by the database. $array["date_created"] does not contain an array of every "date_created" value in the record set; instead, it contains the single "date_created" value for the row you're currently looping on. Same goes for $array["id"], or anything in your $array collection.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Re: Date Diff problem

    print_r($mydate); is correct for every record
    Samba i need the difference of each record individualy so i m gettin each new row

    a bit confused.. i could use mysql_fetch_assoc() but i need to do it this way, stupid i know

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date Diff problem

    saying that it is correct does not help us. that's why I said you should post what it returns...

    :/

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Re: Date Diff problem

    2009-09-21

    2009-09-18

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date Diff problem

    uhh. that is not the format print_r() prints out in. if you want to be helpful, just post whatever was output.

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Re: Date Diff problem

    PHP Code:
    $getarticles="SELECT* FROM InArticlesManagment where issue_id ='$id'";
        
    $art_query=mysql_query($getarticles);
        
    $n=0;
            while (
    $array=mysql_fetch_array($art_query)) {
                
    $titles[]=$array["title$lang"];           $date_published[]=$array["date_created"];
    $ids[]=$array["id"];                                               

    $mydate=$date_published[$n];
    print_r($mydate);
    ....
    .... 

    oh yes it prints the date field like i told you
    ...
    i m trying mate
    thnx

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date Diff problem

    ... lol.

    I am asking what gets output when you run the print_r(). and I mean, I want you to run it, and then copy and paste the part from the print_r(), and then post it.

    that's ALL.

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Re: Date Diff problem

    print $mydate;
    print_r $mydate;

    output

    2009-10-21


  13. #13
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Date Diff problem

    As said previously, $mydate is not an array in this scenario, so print_r() is not printing an array. Here, try it like this...

    Code:
    $art_query=mysql_query($getarticles);
    
    while ($array=mysql_fetch_array($art_query)) {
                
      $id = $array["id"];						    					
      $mydate = explode("-",$array["date_created"]);			
      
      $now  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y"));			
      $pub_date = mktime(0, 0, 0, $mydate[1]+4, $mydate[2], $mydate[0]);
    
      if ($now > $pub_date){
        echo "something";             
      }else{
        echo "something else";               
      }
    }
    Though, with your sample date - "2009-10-21" - then $mydate[1]+4 is 14, and that may not work in mktime() anyway...
    Last edited by SambaNeko; Oct 8th, 2009 at 09:45 AM.

  14. #14
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date Diff problem

    mktime() is smart and will make up for it.

    anyway, "print" is not "print_r()"! since you already stripped some code out of the stuff you've been posting, I had no idea what you might have done to create an array.

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Location
    Athens , Greece
    Posts
    7

    Re: Date Diff problem

    thanks Samba,kows
    had the apocalipsis with explode() earlier

    hm, mktime wont do us the favor i guess

    which function could do the increament we need in the current format
    do i need to change the field to datetime?..

    any suggestions?

  16. #16
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Date Diff problem

    as I said in my last post, mktime() will figure out the correct time when given a month of 14 or even a day of 32, for example. this is all documented on PHP.net, as well. the above example given by SambaNeko would work just fine.

  17. #17
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Date Diff problem

    You're trying to get rows in the database that are less than 4 months old, correct? If you don't need the rest of the rows, then I'd just make MySQL deal with it...

    Code:
    $getarticles = "SELECT * FROM InArticlesManagment WHERE issue_id ='$id' AND date_created >= DATE_SUB(curdate(), INTERVAL 4 MONTH)";
    I think that would work, but haven't tested it myself...

Posting Permissions

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



Click Here to Expand Forum to Full Width