Another Newbie question

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
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

Another Newbie question

Post 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
Last edited by dagee on Sat Oct 21, 2006 8:10 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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';
}
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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';
}
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.
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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';
        }
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

Got it!

Post by dagee »

Thanks Jcart that was the problem.

Dag
Post Reply