Discussion:
If date is greater than
John Taylor-Johnston
2013-10-20 02:44:43 UTC
Permalink
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and
less than december 2012.

Is this the right approach? How bad is my syntax?

|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow|
<= $date2)||)
|| {
|| # do something
|||| }
}
|||
German Geek
2013-10-20 03:30:51 UTC
Permalink
<?php
// assuming | is meant to be a space like this:
function dates_range($todaynow) {
$date1 = strtotime("2011-11-01");
$date2 = strtotime("2012-12-31");
if (($todaynow >= $date1) and ($todaynow <= $date2)) {
# do something
}
}


// I would write:
function betweenDates($dateInQuestion, $afterDate, $beforeDate) {
$after = strtotime($afterDate);
$before = strtotime($beforeDate);
$now = strtotime($dateInQuestion);
return $after <= $now && $now <= $before;
}

// in your code you can then write
if (betweenDates('now', '2011-11-01', '2012-12-31')) {
# do something
}

On 20 October 2013 15:44, John Taylor-Johnston
Post by John Taylor-Johnston
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01"**);
$date2=strtotime("2012-12-31")**;
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow| <=
$date2)||)
|| {
|| # do something
|||| }
}
|||
Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
John Taylor-Johnston
2013-10-20 03:33:35 UTC
Permalink
This works for dates after 2011-11-01 but is not stopping after 2012-12-31.
What is wrong with my if statement?

$meetingdate=strtotime($mydata->meetingdate);
$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (($meetingdate >= $date1) and ($meetingtime <= $date2))
{
$mydata->meetingdate
}
Post by John Taylor-Johnston
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and
less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow|
<= $date2)||)
|| {
|| # do something
|||| }
}
|||
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Taylor-Johnston
2013-10-20 03:46:10 UTC
Permalink
Forget it. I saw my own error.
Post by John Taylor-Johnston
This works for dates after 2011-11-01 but is not stopping after 2012-12-31.
What is wrong with my if statement?
$meetingdate=strtotime($mydata->meetingdate);
$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (($meetingdate >= $date1) and ($meetingtime <= $date2))
{
$mydata->meetingdate
}
Post by John Taylor-Johnston
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and
less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow|
<= $date2)||)
|| {
|| # do something
|||| }
}
|||
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Bastien
2013-10-20 04:00:03 UTC
Permalink
Thanks,

Bastien
Post by John Taylor-Johnston
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow| <= $date2)||)
|| {
|| # do something
|||| }
}
|||
Easiest to convert to integers and then compare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
2013-10-20 08:01:49 UTC
Permalink
Post by Bastien
Thanks,
Bastien
Post by John Taylor-Johnston
I have date strings in my mysql db. yyyy-mm-dd.
I want to parse to see if the date is greater than november 2011 and less than december 2012.
Is this the right approach? How bad is my syntax?
|function dates_range($todaynow)
{ |
|$date1=strtotime("2011-11-01");
$date2=strtotime("2012-12-31");
if (|||($|||||todaynow |>= $date1) and |||||($|||||||todaynow| <= $date2)||)
|| {
|| # do something
|||| }
}
|||
Easiest to convert to integers and then compare
Yes, I was going to ask, why are you storing your dates as strings?
MySQL has a perfectly good DATE type. It's also generally faster
comparing dates within a MySQL query than PHP code.

Thanks,
Ash
http://www.ashleysheridan.co.uk
Tedd Sperling
2013-10-20 15:50:22 UTC
Permalink
Post by Ashley Sheridan
Yes, I was going to ask, why are you storing your dates as strings?
MySQL has a perfectly good DATE type. It's also generally faster
comparing dates within a MySQL query than PHP code.
Thanks,
Ash
http://www.ashleysheridan.co.uk
Agreed.

Plus, there are many date functions provided by MySQL that are easier (possibility faster) than what you can do in PHP.

Check these out:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

tedd

_______________
tedd sperling
***@gmail.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
German Geek
2013-10-22 05:40:11 UTC
Permalink
if you are connected to the db already and the dates in question are in a
table, i agree, using the dbms functions is faster/easier than php.
but if you do not have a connection to the db php is faster.

Tim-Hinnerk Heuer

Twitter: @geekdenz
Blog: http://www.thheuer.com
Post by Tedd Sperling
Post by Ashley Sheridan
Yes, I was going to ask, why are you storing your dates as strings?
MySQL has a perfectly good DATE type. It's also generally faster
comparing dates within a MySQL query than PHP code.
Thanks,
Ash
http://www.ashleysheridan.co.uk
Agreed.
Plus, there are many date functions provided by MySQL that are easier
(possibility faster) than what you can do in PHP.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
tedd
_______________
tedd sperling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...