need help with times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

need help with times

Post by s.dot »

unfortunately, when i first started out with my forums I didn't store timestamps. I stored the date in this format

Code: Select all

Feb 10 at 3:18 PM
Now what I need to do is figure out which year this was in, 2005 or 2006

So I figure I can correct a bunch of the ones from 2005, by determining if the month and day is greater than the month and day of today.

Code: Select all

$p = explode(' ','Feb 10 at 3:18 PM');
$month = $p[0];
$day = $p[1];
This will give me the month and day out of the time.

How can I tell if $month is greater than or less than the month now, and the same with day?

I am familiar with the date() function, I just dont' know how to use it in this instance.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

This is the best I could come up with:

Code: Select all

$p = explode(' ',$topicsa['lastposttime']);
	$month = $p[0];
	$day = $p[1];
	
	switch($month){
		
		case 'Jan':
		$month = 1;
		break;
		
		case 'Feb':
		$month = 2;
		break;
		
		case 'Mar':
		$month = 3;
		break;
		
		case 'Apr':
		$month = 4;
		break;
		
		case 'May':
		$month = 5;
		break;
		
		case 'Jun':
		$month = 6;
		break;
		
		case 'Jul':
		$month = 7;
		break;
		
		case 'Aug':
		$month = 8;
		break;
		
		case 'Sep':
		$month = 9;
		break;
		
		case 'Oct':
		$month = 10;
		break;
		
		case 'Nov':
		$month = 11;
		break;
		
		case 'Dec':
		$month = 12;
		break;
		
	}
	$thismonth = date("n");
	$thisday = date("d");
	
	if($month == $thismonth){
		if($day > $thisday){
			$year = 2005;
		} else {
			$year = 2006;
		}
	} elseif($month > $thismonth){
		$year = 2005;
	} else {
		$year = 2006;
	}
	
	$datewithyear = $p[0].' '.$p[1].' '.$year.' '.$p[2].' '.$p[3].' '.$p[4];
If anyone else knows of a better way, let me know.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

scottayy wrote:This is the best I could come up with:
If anyone else knows of a better way, let me know.
It's not really sophisticated, but at least it works (and clogs up fewer lines) :)

Code: Select all

<?php
$string     = "May 22 at 3:18 PM";
$p          = explode('at',$string);
$d          = explode(' ',$p[0]);

$montharray = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

$cmonth     = date ("n") - 1;
$cday       = date ("j");
$key        = array_search ($d[0], $montharray);

if ($key > $cmonth || ($key == $cmonth && $d[1] > $cday)) {
  echo """.$string."" should be ".$p[0]."2005".$p[1]."<br />";
  $newstring = $p[0]."2005".$p[1];
}
else {
  echo """.$string."" should be ".$p[0]."2006".$p[1]."<br />";
  $newstring = $p[0]."2006".$p[1];
}

$oldstamp = strtotime($newstring);
echo "resulting in the timestamp: ".$oldstamp." (check: ".date("F j, Y, g:i a",$oldstamp).")";  
?>
aerodromoi
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

<?php
$input = 'Jun 10 at 3:18 PM';

$dateparts = explode(' at ', $input);

if (strtotime($dateparts[0]) < time()) {
	echo 'date in the past';
} else {
	echo 'date in the future';
}
?>
Post Reply