Another Newbie question
Moderator: General Moderators
Another Newbie question
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
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.
Have a look at http://www.php.net/strtotime.
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...
Code: Select all
<?php
echo date("y-m-d", strtotime("+1 day"));
?>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?
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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';
}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
dag
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.
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?
[/syntax]
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';
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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';
}