Get first second of today, past days

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Get first second of today, past days

Post by phice »

Code: Select all

function getFirstSecond($days = 1, $time = 0)
	{
		// limitation of functions inside function creation
		if($time == 0)
		{
			$time = time();
		}
		
		// Get the first second
		$hours = date("G", $time);
		$minutes = date("i", $time);
		$seconds = date("s", $time);
		
		$temp = $time;
		$temp -= ($hours * 3600);
		$temp -= ($minutes * 60);
		$temp -= $seconds;
		
		$today = $temp;
		$first = $today;
		
		$first -= ( ($days - 1) * 86400 );
		
		return $first;
	}
You can use this program to retrieve something like all news listings from today, or specify a number of days (including today) like: getFirstSecond(4), which will give you the first second of three days ago (not including today).

Confusing? I thought so.

Example:

Code: Select all

print "Today's Start: " . date("r", getFirstSecond());
	print "<br /><br />\n\n";
	print "Past 3 Days (today counts as one): " . date("r", getFirstSecond(3));
which will result in:
Today's Start: Thu, 8 Jul 2004 00:00:00 -0500

Past 3 Days (today counts as one): Tue, 6 Jul 2004 00:00:00 -0500
Real world use: lets say the column in table 'news' is the time() of creation of that row. In the query, just do:

Code: Select all

SELECT * FROM `news` WHERE time >= '" . getFirstSecond(3) . "'
Which will grab the headlines from today, yesterday, and the day before (3 days).
Image Image
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

i could use somethinglike this.. nice job :)
Post Reply