RyanSF07 Posted September 15, 2009 Share Posted September 15, 2009 Hello, I found a php class online that I'd like to use but I'm not keen enough to successfully pass the myrow data to the darned class. The code I have working now takes the myrow data generated from a while clause, passes it through a function and spits out the "date posted" in a table cell. I'd like to try something different.. but anyway, this what I have is working: echo "<td align='right'>"; dateposted($myrow['date']); I'd like to pass it to this class which begins: <?php class cktsTime { public static function timeAgo( $datefrom, $dateto = -1 ) { if ( ($datefrom = intval($datefrom)) <= 0) return('A long time ago.'); if ( ($dateto = intval($dateto)) <= 0) $dateto = time(); if ( ($difference = ($datefrom - $dateto)) <= 0) return('NO time AT ALL elapsed so far.'); ...and on and on.... The class comes with notes to run this: $timeElapsedUntilNowAsString = cktsTime::timeAgo($datereceived); echo('this bla bla bla is '.$timeElapsedUntilNowAsString); But everything I've tried to pass the myrow data to the timeAgo function fails. How do I pass this: dateposted($myrow['date']); to the timeAgo function rather than the dateposted function? I thought it would be as simple as: timeAgo($myrow['date']); or cktsTime::timeAgo($myrow['date']); I also tried setting the $datereceived variable equal to the myrow data in several different places, but that hasn't worked either. Just confused. Trying to make this work. Thank you in advance for your help pointing me in the right direction. Ryan Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/ Share on other sites More sharing options...
RussellReal Posted September 15, 2009 Share Posted September 15, 2009 $class = cktsTime(); $class->timeAgo($myrow['date']); I guess.. if thats how it shoulda been Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-918733 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Thank you, Russel. with that I'm getting a similar error: Fatal error: Call to undefined function cktsTime() I also tried: $class = public static function(); $class->timeAgo($myrow['date']); $class; and that produces the error: Parse error: syntax error, unexpected T_PUBLIC I also tried: $class = class cktsTime(); $class->timeAgo($myrow['date']); $class; that produces the error: Parse error: syntax error, unexpected T_CLASS Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-918954 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 I also tried setting the $datefrom variable equal to myrow['date']. Seems that would need to be done, but if the function isn't even being called that wouldn't set anyway, right? ... confusing. thanks for your help sorting this out. Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-918967 Share on other sites More sharing options...
RussellReal Posted September 15, 2009 Share Posted September 15, 2009 I'm sorry.. do $class = new cktsTime(); Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-918982 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Thank you, Russel. I tried this: $class = new cktsTime(); $class->timeAgo($myrow['date']); echo "$class"; and now get this error: Catchable fatal error: Object of class cktsTime could not be converted to string Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-918985 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 There are some notes included in this class script that I had removed previously for easier reading. Now it seems that for this to work, I need to define @Params -- how do I do that? In my database, the "date" data is in "timestamp" format. Here is the beginning of that script again with the notes: class cktsTime { /** * Calculates elapsed time * * Uses UNIX timestamp(s) to perform comparisons. * Works on a basis timefrom/timeto, whereas timeto is NOW, if argument is omitted. * * @param int $datefrom REQUIRED time(stamp) FROM which elapsed time has to be calculated * @param int $dateto OPTIONAL time(stamp) TO which elapsed time has to be calculated, defaults to * CURRENT TIME (as returned by PHP time()) * @return */ public static function /** @var char*/ timeAgo( /** @var int*/ $datefrom, /** @var int*/ $dateto = -1 ) { // assume an error if invalid required argument is passed if ( ($datefrom = intval($datefrom)) <= 0) return('A long time ago.'); // typecheck optional arg if ( ($dateto = intval($dateto)) <= 0) $dateto = time(); I don't want to give this up unless this script simply won't work with my data for one reason or another. I'm just trying to understand the next steps -- googling and reading up online, but still not making the connection. How do I set this Param values? Thank you in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919057 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 the @param notes in the class just talk about what parameters go into the constructor function. You seem to be calling to contructor right, your problem is echoing out the data correct? try $class = new cktsTime(); $time = $class->timeAgo($myrow['date']); echo "$time"; from the little bit of that class I saw, it seems like that time_ago function returns the value of how long its been. Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919059 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 thanks. I'll try this right now. else where in the script notes it reads: * Serving static method(s) only, thus comes withOUT a Construictor or any members Thanks -- will post back soon Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919062 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 yeah I realized after I posted that that method wasnt the constructor. ignore what I said about constructor functions Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919064 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Holly crap -- getting close! That bit actually connected to the class and ran through to here: $dateto = time(); // get arithmetic difference between both times // protect from "underflow" if ( ($difference = ($datefrom - $dateto)) <= 0) return('NO time AT ALL elapsed so far.'); "No time at all elapsed" was returned. Does that mean that the data in the database is somehow being set as the "dateto" value, and then compared to the "dateto" (now) value? Thanks, Mike. Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919075 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 that means that whatever value you are passing as $datefrom is the same as $dateto, or rather the time you are passing is basically the current time. Can i see the code you have? Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919084 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Sure, I found it here: http://www.webdeveloper.com/forum/showthread.php?t=192324 Are "timestamp" and "time()" both formatted the same, or do I have to convert the timestamp data into "time()" format? Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919089 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 nevermind -- from what I've found online they both represent the number of seconds since a date in 1970... Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919092 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 nope not at all. time() is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). try using strtotime on your date before you pass it in. for example, given the following code $time = date("Y-m-d h:i:s");//timestamp format $date = strtotime($time); $times = time(); echo $time."<br />"; echo $date."<br />"; echo $times; would output: 2009-09-15 04:03:32 1253001812 1253045012 Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919094 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 well, I tried this: $class = new cktsTime(); $time = $class->timeAgo(strtotime($myrow['date'])); echo "$time"; and this: $time = $myrow['date']; $date = strtotime($time); $class = new cktsTime(); $times = $class->timeAgo($date); echo "$times"; Unfortunately I got the same message -- "No time elapsed.." hmmm... did I use strtotime correctly? Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919103 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 you are using it correctly. echo $time and see what its value is. Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919105 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Thanks, Mike. I echoed both $time and $date from the second block of above code and got: 2009-09-12 12:16:41 1252783001 What do you think at this point. Broken class script and close the topic? Or try something else to troubleshoot this? Thanks, Ryan Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919121 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 hmm ok that seems right. post the whole function from the class that you are using. also, echo out regular time, like echo time(); and see what that says. Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919127 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 So here's what I'm calling / echoing: $time = $myrow['date']; $date = strtotime($time); $class = new cktsTime(); $times = $class->timeAgo($date); echo "$times <br>"; echo "$time <br>"; echo "$date <br>"; echo time(); and here are the results: NO time AT ALL elapsed so far. 2009-09-12 12:16:41 1252783001 1253048170 Here too is the whole script (below). Thank you very much for your help! <?php // file: cktsTime.class.php /** * Supposed to manage various date/time isues * * Serving static method(s) only, thus comes withOUT a Construictor or any members * */ class cktsTime { /** * Calculates elapsed time * * Uses UNIX timestamp(s) to perform comparisons. * Works on a basis timefrom/timeto, whereas timeto is NOW, if argument is omitted. * * @param int $datefrom REQUIRED time(stamp) FROM which elapsed time has to be calculated * @param int $dateto OPTIONAL time(stamp) TO which elapsed time has to be calculated, defaults to * CURRENT TIME (as returned by PHP time()) * @return */ public static function /** @var char*/ timeAgo( /** @var int*/ $datefrom, /** @var int*/ $dateto = -1 ) { // assume an error if invalid required argument is passed if ( ($datefrom = intval($datefrom)) <= 0) return('A long time ago.'); // typecheck optional arg if ( ($dateto = intval($dateto)) <= 0) $dateto = time(); // get arithmetic difference between both times // protect from "underflow" if ( ($difference = ($datefrom - $dateto)) <= 0) return('NO time AT ALL elapsed so far.'); // come to here, $difference holds the "raw" value in SECONDS // default return value with SECONDS, // this saves ONE check, namely for "< 60" $c = 's'; // for readability, define some defaults $min = 60; $hour = ($min * 60); $day = ($hour * 24); $week = ($day * 7); $month = ($day * 30); $year = (intval($day * 366.5)); // check for a difference like between one MIN and one HOUR if ( ($difference >= $min) && ($difference < $hour)) $c = 'n'; // check for a difference like between one HOUR and one DAY elseif ( ($difference >= $hour) && ($difference < $day)) $c = 'h'; // check for a difference like between one DAY and one WEEK elseif ( ($difference >= $day) && ($difference < $week)) $c = 'd'; // check for a difference like between one WEEK and one MONTH elseif ( ($difference >= $week) && ($difference < $month)) $c = 'ww'; // check for a difference like between one MONTH and one YEAR elseif ( ($difference >= $month) && ($difference < $year)) $c = 'm'; else $c = 'y'; /* * Based on the interval, determine the * number of units between the two dates * From this point on, you would be hard * pushed telling the difference between * this function and DateDiff. If the $datediff * returned is 1, be sure to return the singular * of the unit, e.g. 'day' rather 'days' */ // default return char $retchar = ''; // determine interval switch($c) { // switch case "m": { // month $months_difference = floor($difference / 60 / 60 / 24 / 29); // generate as lon as current time (dateto) hasn't been reached while ( mktime( date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom) ) < $dateto ) $months_difference++; // assign difference $datediff = $months_difference; /** * We need this in here because it is possible * to have an 'm' interval and a months * difference of 12 because we are using 29 days * in a month */ if($datediff==12) $datediff--; $retchar = 'month'; break; } // month case "y": { // year $datediff = floor($difference / 60 / 60 / 24 / 365); $retchar = 'year'; break; } // year case "d": { // day $datediff = floor($difference / 60 / 60 / 24); $retchar = 'day'; break; } // day case "ww": { // week $datediff = floor($difference / 60 / 60 / 24 / 7); $retchar = 'week'; break; } // week case "h": { // hour $datediff = floor($difference / 60 / 60); $retchar = 'hour'; break; } // hour case "n": { // minute $datediff = floor($difference / 60); $retchar = 'minute'; break; } // minute case "s": { // second $datediff = $difference; $retchar = 'second'; break; } // second } // switch // take care of proper grammar, // thus month/months, year/years, and so on if ($datediff != 1) $retchar .= 's'; // send home... // creating a "value" is actually overhead // however, certain environments REQUIRE an ASSIGNMENT prior to return, // so, for safety's sake... return( ($c = $datediff.' '.$retchar.' ago')); } // END timeAgo() } // END CLASS // class above is easy to use // EXAMPLE /** * You receive a date (from where ever) as a timestamp * if you want to know time elapsed until NOW, you simply pass that * timestap the (static) class method * NOTE: I certainly assume $datereceived to being retrieved from somewhere... */ $timeElapsedUntilNowAsString = cktsTime::timeAgo($datereceived); echo('this bla bla bla is '.$timeElapsedUntilNowAsString); // puts out 'this bla bla... is one week ago /** * In case you have a second time (prior to NOW), you passed that one, too. * Again, assuming there is a value (int) $datetocompareto * */ $timeElapsedBetweenDatesAsString = cktsTime::timeAgo($datereceived,$datetocompareto); /** * The good things (or ONE of them) with classes is, that you can extend them at any time, NOT * breaking any code written prior to changes. * * Have Fun, * ariell. */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919135 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 hmm, well whats happening is that when the function subtracts your input date from the value of time() it returns a negative number. whoever wrote this function didn't do it right I think (well the logic anyways) but try this $times = $class->timeAgo(time(), $date); and see what happens Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919140 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Hi Mike, You did it. It's working now. Thank you very much. The only thing that is weird is that when I post a comment, it returns "0 years ago" -- ha! Then if I wait two minutes and refresh the page it retures, "2 minutes ago" do you see a code tweak to fix that? thank you again so much! Ryan Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919150 Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 curious. thats because of this particular snippet of code from the functin // check for a difference like between one MIN and one HOUR if ( ($difference >= $min) && ($difference < $hour)) $c = 'n'; // check for a difference like between one HOUR and one DAY elseif ( ($difference >= $hour) && ($difference < $day)) $c = 'h'; // check for a difference like between one DAY and one WEEK elseif ( ($difference >= $day) && ($difference < $week)) $c = 'd'; // check for a difference like between one WEEK and one MONTH elseif ( ($difference >= $week) && ($difference < $month)) $c = 'ww'; // check for a difference like between one MONTH and one YEAR elseif ( ($difference >= $month) && ($difference < $year)) $c = 'm'; else $c = 'y'; it seems that all the checks are running false on the first run, and then running fine on the second. I don't really know why it does that, but I can't think of a quick fix off the top of my head. If it works on the second time around but not the first, i'm not sure why it would do that. Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919153 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Yep. I screwed around with it for the past 20 minutes or so and -- looks like this part isn't rendering or parsing correctly: case "s": { // second $datediff = $difference; $retchar = 'second'; break; } // second } // switch would it be $datediff = $difference;, though -- wouldn't it be "less than" 60 but greater than 1 ? I don't know. If you have time to revisit it and offer some suggestions, great. Thank you again for your help getting this to work Mike. Cheers, Ryan Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919176 Share on other sites More sharing options...
RyanSF07 Posted September 15, 2009 Author Share Posted September 15, 2009 Found a fix --- After that if statement that kept throwing the "No time at all has elapsed" error I added: if ( ($difference = ($datefrom - $dateto)) <= 60) return('Less than a minute ago.'); That fixed the "0 years ago" problem. Thanks again Mike all for your help. Glad to have this working! Quote Link to comment https://forums.phpfreaks.com/topic/174267-solved-help-passing-myrow-data-to-a-phpclass/#findComment-919195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.