Jump to content

Matching two strings


leke

Recommended Posts

Hi, For some I can't match these two strings...

 

$timestamp = mktime() + CURR_TIME_OFFSET * 3600;
$d = date('j', $timestamp);  
$m = date('n', $timestamp);  
$y = date('Y', $timestamp);
$isCancelledCheck = (string) "$y-$m-$d";
echo "Date: $isCancelledCheck is a " . gettype($isCancelledCheck) . "<br>";

$db_read = file('cancelled_dates.txt'); // read YYYY-MM-DD lines of file to an array.

foreach($db_read as $key => $val) {
    // iterate through each element of db_read
    if ($val == $isCancelledCheck){
        echo "<br> $val == $y-$m-$d";
    } else {
        echo "<br>Skipped $val";
    }    
}

print_r($db_read);
 

 

 

which returns...


 

Date: 2013-3-4 is a string

Skipped 2013-4-4 is a string
Skipped 2013-3-4 is a string
Skipped 2012-3-4 is a string
Skipped 2013-3-5 is a string
Skipped 2013-2-4 is a string

 

Array ( [0] => 2013-4-4 [1] => 2013-3-4 [2] => 2012-3-4 [3] => 2013-3-5 [4] => 2013-2-4 )

 

How come the string 2013-3-4 is not matched here?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/275226-matching-two-strings/
Share on other sites

When you really want to know exactly what a function contains, it is generally var_dump that you want to use. Add an echo "<pre>"; in front of it, or check the page source, to see all whitespace as well. (Browsers tends to collapse whitespace into a single space, after all.)

so why are you formatting yours as YYYY-M-D

To be honest, I didn't really give it much thought :D I was just going with something that works quick, then I usually play with it and make some changes to the formatting based on problems I might foresee. I usually go with year month day as it's usually the most computer friendly format (along with being quite readable and international), but I also see I made an error in the comment writing mm-dd instead of m-d.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.