Date and time functions

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

aragon41
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Aug 09, 2014 2:24 am

Date and time functions

Postby aragon41 » Sat Aug 09, 2014 2:33 am

Hi,

I tried to run the below php code:
<?php
echo checkdate(07, 31, 2014) ? 'true':'false';
echo "<br />";
echo checkdate(08, 01, 2014) ? 'true':'false';
echo "<br />";
?>

Output:
true
false


Please help me understand why its showing false for the second checkdate function..

landi
php-forum Active User
php-forum Active User
Posts: 258
Joined: Thu Mar 15, 2012 3:59 pm

Re: Date and time functions

Postby landi » Sat Aug 09, 2014 3:30 am

Hi,
It's because 08 is treated as an invalid octal number (it's a throwback to C - numbers starting with a leading 0 are treated as octal numbers). 07 happens to work because 07(base8) == 7 (base 10)
This will work:

Code: Select all

echo checkdate(8, 01, 2014) ? 'true':'false';

So will this:

Code: Select all

echo checkdate('08', 01, 2014) ? 'true':'false';


-A

aragon41
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Aug 09, 2014 2:24 am

Re: Date and time functions

Postby aragon41 » Sat Aug 09, 2014 7:25 am

Thanks for your reply, landi.

If possible can you please also solve these two problems

echo "HTTP_REFERER: " . $_SERVER['HTTP_REFERER'];

Error on above code:
Notice: Undefined index: HTTP_REFERER in C:\xampp\h........


Problem 2:

$a = 1;
$b = & $a;
$b = 2;
echo "a : {$a}/ b : {$b}<br />";
// returns 2/2

unset($b);
echo "a : {$a}/ b : {$b}<br />";


Error on the above code:
Notice: Undefined variable: b in C:\xamp.............

aragon41
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Aug 09, 2014 2:24 am

Re: PHP server functions and unset function error

Postby aragon41 » Sat Aug 09, 2014 8:03 am

Problem 1:

echo "HTTP_REFERER: " . $_SERVER['HTTP_REFERER'];

Code: Select all

Error on above code:

Notice: Undefined index: HTTP_REFERER in C:\xampp\h........


Problem 2:

$a = 1;
$b = & $a;
$b = 2;
echo "a : {$a}/ b : {$b}<br />";
// returns 2/2

unset($b);
echo "a : {$a}/ b : {$b}<br />";


Code: Select all

Error on the above code:

Notice: Undefined variable: b in C:\xamp.............

landi
php-forum Active User
php-forum Active User
Posts: 258
Joined: Thu Mar 15, 2012 3:59 pm

Re: Date and time functions

Postby landi » Sat Aug 09, 2014 9:47 am

Hi,

1) $_SERVER['HTTP_REFERER'] is not guarenteed to be set (see docs). You can test first with isset()

2) Not sure why you expect any other result:

Code: Select all

unset($b); // b is now an undefined variable


-A


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 1 guest

cron