|
-
Oct 7th, 2009, 03:54 PM
#1
Thread Starter
New Member
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
-
Oct 7th, 2009, 04:09 PM
#2
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?
-
Oct 7th, 2009, 04:29 PM
#3
Thread Starter
New Member
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
-
Oct 7th, 2009, 04:45 PM
#4
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).
-
Oct 7th, 2009, 04:48 PM
#5
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.
-
Oct 7th, 2009, 04:58 PM
#6
Thread Starter
New Member
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
-
Oct 7th, 2009, 06:08 PM
#7
Re: Date Diff problem
saying that it is correct does not help us. that's why I said you should post what it returns...
:/
-
Oct 7th, 2009, 06:14 PM
#8
Thread Starter
New Member
-
Oct 7th, 2009, 06:18 PM
#9
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.
-
Oct 8th, 2009, 02:42 AM
#10
Thread Starter
New Member
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
-
Oct 8th, 2009, 08:15 AM
#11
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.
-
Oct 8th, 2009, 08:26 AM
#12
Thread Starter
New Member
Re: Date Diff problem
print $mydate;
print_r $mydate;
output
2009-10-21
-
Oct 8th, 2009, 09:42 AM
#13
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.
-
Oct 8th, 2009, 10:04 AM
#14
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.
-
Oct 8th, 2009, 03:09 PM
#15
Thread Starter
New Member
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?
-
Oct 8th, 2009, 03:41 PM
#16
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.
-
Oct 8th, 2009, 03:58 PM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|