Mktime problem

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Mktime problem

Post by spacebiscuit »

Hi,

i've juyst recently switched to a new server (with a newer version of PHP I believe) and now the following code is not working:

Code: Select all

$date=explode("-", $temp);
$date[0]=date('y',mktime(0,0,0,$date[1],$date[2],$date[0]));
I am receiving the following error:

Warning: mktime() expects parameter 4 to be long, string given

I have tested the content of the variables:

Code: Select all

echo"$temp = $date[1] $date[2] $date[0]";
which gives:

2009-12-12 = 12 12 2009

Oddly only data[1] is reporting as a string and not data[0] or data[2].

Any ideas?

Thanks in advance,

Rob.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Mktime problem

Post by flying_circus »

The problem appears to be the datatype related. Parameter 4 of mktime() appears to be $date[1] which is apparently a string, but mktime is looking for an integer.

In a strict environment "12" != 12, but PHP is supposed to be loose as far as datatypes go.

Try casting it to an int (though, this may not give the predicted output):

Code: Select all

<?php
  $date[0]=date('y',mktime(0,0,0,(int) $date[1],$date[2],$date[0]));
?>
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Mktime problem

Post by spacebiscuit »

Thanks - type castin the variable did the trick. Any ideas why this index of the array is being read as a string though? I could understand if the other indexes were also read as this data type, but why the second only?

Rob.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Mktime problem

Post by flying_circus »

I'm not sure. Though, looking over your code again, you might check out strtotime(). It may save you a little coding, but you'll have to test it for the desired results.

Code: Select all

<?php
# One way of doing things
  $date=explode("-", $temp);
  $date[0]=date('y',mktime(0,0,0,$date[1],$date[2],$date[0]));
 
# Another way of doing things
  $date=date('y',strtotime($temp));
?>
Post Reply