Page 1 of 1
need help with times
Posted: Sat May 20, 2006 7:28 pm
by s.dot
unfortunately, when i first started out with my forums I didn't store timestamps. I stored the date in this format
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.
Posted: Sat May 20, 2006 7:54 pm
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.
Posted: Sun May 21, 2006 4:28 am
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
Posted: Sun May 21, 2006 4:53 am
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';
}
?>