Page 1 of 1

Another Newbie question

Posted: Sat Oct 21, 2006 5:14 pm
by dagee
Hi,

Really quick question. I am trying to write a php script that takes the current date and displays the next day's date. I know how to have php display the current date but how do I manipulate this date. originally I had tried:

<?php
echo date("m.d+1.Y");
?>

but that did not work (I'll admit I did not think it would).

Second question, when php looks at the date function, how does it interpret it? For example let's say I wanted to test if today's date was a certain date so I wrote: an if statment comparing a given string to the current date. This does not work (I tried) so what would need to be done?

example:
$currentDate= date("m.d.Y");
$date = 08.09.2006;
if ($currentDate==$date)
.
.
.
This does not work. I am assuming because the date function returns a different type that cannot be compared to a numeric string. I thought I would try to unserialize the date ($currentDate) (but that did not work: which does not surprise me since I really don't quite get the unserialize concept.

Thanks in advance for any advice given and expanations rendered.

Dag

Posted: Sat Oct 21, 2006 5:19 pm
by timvw
Have a look at http://www.php.net/strtotime.

Code: Select all

<?php
echo date("y-m-d", strtotime("+1 day"));
?>
If you want to 'compare' dates in php i suggest that you convert the string representations to unixtimestamps. And then work with that numeric representation...

Posted: Sat Oct 21, 2006 6:19 pm
by dagee
Thank you so much for pointing in me to the strtotime function - that really helped. Follow up question, if you don't mind: I can see how to compare to dates using the strtotime function but how do I compare a date gotten from the strtotime function to a user inputted date.

For examle,

The user inputs --> 10.21.2006 for the date and I want to check to see if this is the is the same as the date strtotime("day") are the same. How do I do this?

Posted: Sat Oct 21, 2006 6:42 pm
by John Cartwright
pass the user input to strtotime() like you previous did, ie

Code: Select all

$input = '10.21.2006';

$date = strtotime($input, time());

if (!$date) {
   echo 'You have entered an invalid date format';
}

Posted: Sat Oct 21, 2006 7:59 pm
by dagee
Ok. First of I'm getting there. Thank you so much with everyone's patience. I did what jcart said but my desired outcome is still not working. I think the problem is that the strtotime function gets the exact server time. When I try to compare it to a user's input which only is specifies month, year, and day, my program cannot tell if both are equal (or should I say, it can tell me that they are not equal since the strtotime function is comparing the exact time to the vague time the user is inputting. Am I correct? Is this what is happening? What do I need to do inorder to get my program to confirm that two days are correct without regards to time?

dag

Posted: Sat Oct 21, 2006 9:43 pm
by s.dot
strtotime() of the two same dates should return the same timestamp regardless of what time it is

Code: Select all

$userinput = '10-22-2006';
$today = '10-22-2006';

if(strtotime($userinput) == strtotime($today))
{
   echo 'dates are the same';
} else
{
   echo 'dates are not the same';
}

Posted: Sat Oct 21, 2006 10:27 pm
by dagee
So I tried scottay's with just a slight change because I want to compare the input date to the actual date. My output though says that the two dates are the same even though they are clearly not. What am I doing wrong?

Code: Select all

<?php
	$userinput = '10.24.2006'; 
	$today = date("m.d.Y"); //today's date is 10.21.2006

	if(strtotime($userinput) == strtotime($today)) 
	{ 
		echo 'dates are the same'; 
	}
	else 
	{ 
		echo 'dates are not the same'; 
	} 
?>
[/syntax]

Posted: Sun Oct 22, 2006 11:15 am
by John Cartwright
It seems '10.24.2006' is not a format you can pass to strtotime(). Run a str_replace to remove the dots with forward slashes.

Code: Select all

$userinput = '10.24.2006';
        $userinput = str_replace('.', '/', $userinput);

        if(strtotime($userinput) == strtotime('Today', time())
        {
                echo 'dates are the same';
        }
        else
        {
                echo 'dates are not the same';
        }

Got it!

Posted: Sun Oct 22, 2006 11:55 am
by dagee
Thanks Jcart that was the problem.

Dag