Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 8th, 2013, 04:14 AM
Registered User
Points: 90, Level: 1
Points: 90, Level: 1 Points: 90, Level: 1 Points: 90, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Exclamation Undefined offset error agoTimeFormat class

Quote:
Hi Everyone,
i am creating agoTimeFormat class in php but i am getting error in line 16 and 17, here is my code.
PHP Code:
<?php
class convertToAgo {

function 
convert_datetime($str) {
            list(
$date$time) = explode(' '$str);
        list(
$year$month$day) = explode('-'$date);
        list(
$hour$minute$second) = explode(':'$time);
        
$timestamp mktime($hour$minute$second$month$day$year);
        return 
$timestamp;
}

function 
makeAgo($timestamp){
           
$difference time() - $timestamp;
           
$periods = array("sec""min""hr""day""week""month""year""decade");
           
$lengths = array("60","60","24","7","4.35","12","10");
           for(
$j 1$difference >= $lengths[$j]; $j++)
               
$difference /= $lengths[$j];
               
$difference round($difference);
           if(
$difference != 1$periods[$j].= "s";
               
$text "$difference $periods[$j] ago";
               return 
$text;
}
}
?>
Quote:
OUTPUT WITH ERROR:
Notice: Undefined offset: 7 in C:\xampp\htdocs\class\agoTimeFormat.php on line 16

Notice: Undefined offset: 7 in C:\xampp\htdocs\class\agoTimeFormat.php on line 17

Warning: Division by zero in C:\xampp\htdocs\class\agoTimeFormat.php on line 17

Notice: Undefined offset: 8 in C:\xampp\htdocs\class\agoTimeFormat.php on line 16
...
...
...
Notice: Undefined offset: 18208 in C:\xampp\htdocs\class\agoTimeFormat.php on line 17

Warning: Division by zero in C:\xampp\htdocs\class\agoTimeFormat.php on line 17

Notice: Undefined offset: 18209 in C:\xampp\htdocs\class\agoTimeFormat.php on line 16

Notice: Undefined offset: 18209 in C:\xampp\htdocs\class\agoTimeFormat.php on line 17

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\class\agoTimeFormat.php on line 17

plz help i m getting 20k error, Thanks
Reply With Quote
  #2 (permalink)  
Old June 11th, 2013, 03:24 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Fixed:
Code:
<?php

class convertToAgo
{

	function convert_datetime($str)
	{
		list($date, $time) = explode(' ', $str);
		list($year, $month, $day) = explode('-', $date);
		list($hour, $minute, $second) = explode(':', $time);
		$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

		return $timestamp;
	}

	function makeAgo($timestamp)
	{
		$difference = time() - $timestamp;

		$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
		$lengths = array("60","60","24","7","4.35","12","1","10");

		$text = '';
		for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
		{
			$difference /= $lengths[$j];
		}

		$difference = round($difference);

		if($difference != 1)
		{
			$periods[$j].= "s";
		}

		$text = "$difference $periods[$j] ago<br />";

		return $text;
	}
}

$ago = new convertToAgo();

// Specify date which should be in YYYY-MM-DD HH:MM:SS format
$date = '1972-04-14 17:10:33';

// create the timestamp for the specified date
$ts = $ago->convert_datetime($date);

// do conversion
echo $ago->makeAgo($ts);

?>
Reply With Quote
  #3 (permalink)  
Old June 11th, 2013, 04:30 AM
Registered User
Points: 90, Level: 1
Points: 90, Level: 1 Points: 90, Level: 1 Points: 90, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default Still Same error............................

Quote:

-12597 secs ago

its gives in negative value. Why?
PHP Code:
-12597 secs ago

its gives in negative value
Why
Reply With Quote
  #4 (permalink)  
Old June 11th, 2013, 04:43 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

No it's not the same error.

You're getting a negative value because the date/time is in the future.

For example using:
Code:
2013-06-11 10:10:33
on the 11-06-2013 at 09:38 results in: -1999 secs ago

But using:
Code:
2013-04-11 10:10:33
on the 11-06-2013 at 09:40 results in: 2 months ago

and using:
Code:
2013-06-11 08:10:33
on the 11-06-2013 at 09:41 results in: 2 hours ago

Therefore your class can only handle dates/times in the past if you want it to handle future dates/times you'll need to cater for this.
Reply With Quote
The Following User Says Thank You to UseLess For This Useful Post:
farwall.ghost (June 11th, 2013)
  #5 (permalink)  
Old June 11th, 2013, 05:12 AM
Registered User
Points: 90, Level: 1
Points: 90, Level: 1 Points: 90, Level: 1 Points: 90, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default ok... i understand

PHP Code:
so what is the actual solution
Reply With Quote
  #6 (permalink)  
Old June 11th, 2013, 06:45 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Change the function to test the timestamp to see if it's greater than the current time if it is take the timestamp away from the current time and set a string to be 'ago' <else> take the current time away from the timestamp and set the string to be 'from now' for example.
Reply With Quote
The Following User Says Thank You to UseLess For This Useful Post:
farwall.ghost (June 11th, 2013)
  #7 (permalink)  
Old June 11th, 2013, 01:06 PM
Registered User
Points: 90, Level: 1
Points: 90, Level: 1 Points: 90, Level: 1 Points: 90, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default ok, thanks......

PHP Code:
i think i have used DATETIME datatype instead of using TIMESTAMP type in sql while creating fields in table.

is this so
Reply With Quote
  #8 (permalink)  
Old June 12th, 2013, 02:32 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

And we would know you've used the wrong datatype how?

And you alter the function like this;
Code:
	function makeAgo($timestamp)
	{
		$now = time();

		if( $now > $timestamp )
		{
			$difference = $now - $timestamp;
			$string = 'ago';
		}
		else
		{
			$difference = $timestamp - $now;
			$string = 'from now';
		}

		$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
		$lengths = array("60","60","24","7","4.35","12","10");

		for($j = 0, $j_end = count($lengths)-1; $difference >= $lengths[$j] && ($j < $j_end); $j++)
		{
			$difference /= $lengths[$j];
		}

		$difference = round($difference);

		$interval = $periods[$j] . ( ($difference != 1) ? 's' : '');

		$text = "$difference $interval {$string}<br />";

		return $text;
	}
Reply With Quote
The Following User Says Thank You to UseLess For This Useful Post:
farwall.ghost (June 16th, 2013)
  #9 (permalink)  
Old June 16th, 2013, 02:38 AM
Registered User
Points: 90, Level: 1
Points: 90, Level: 1 Points: 90, Level: 1 Points: 90, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default thank you so much...

PHP Code:
wellThanks for help,

i got error of negative value in localhost only when i uploaded it to internet its working perfactly
thanks 
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined Offset 7thsense101 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 December 9th, 2009 06:49 AM
Re: Undefined offset: 1 chapter 5 form.php sreenivasharish BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 November 9th, 2008 06:08 PM
undefined offset error trev Beginning PHP 9 January 24th, 2007 04:41 AM
Notice: Undefined offset: ## Herjan BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 27th, 2005 12:29 PM
ImageLine Undefined offset error ss2003 Beginning PHP 3 January 13th, 2004 04:37 PM



All times are GMT -4. The time now is 10:25 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.