Forum Moderators: coopster

Message Too Old, No Replies

mktime() Question

Using data from date() function

         

StupidScript

9:58 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$thisdate=date("m,d,Y",strtotime("24 Jan 2005"));

echo $thisdate."<br />";

echo mktime(00,01,12,$thisdate)."<br />";

echo mktime(00,01,12,01,24,2005)."<br />";

echos:

01,24,2005 <= $thisdate

1104912072 <= first mktime using $thisdate

1104825672 <= 2nd mktime using same data directly

Using:

echo strftime("%c",mktime(00,01,12,$thisdate));

echo strftime("%c",mktime(00,01,12,01,24,2005));

it is clear that

mktime()
is stuck on "today" as the day, even though its reading the month and year correctly.
mktime()
seems to be having trouble getting the day right when I use data from the
date(..,strtotime())
method.

Similarly, if I set a variable to hold the time info, i.e.

$thistime="00,01,12";
and then try

$getdate=$thistime.$thisdate;

echo mktime($getdate);

I consistently echo the current time and date.

Anyone care to venture a guess as to why?

Running Win 98SE 4.10, PHP 4.2.3, Apache 1.3.12

StupidScript

10:10 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did try
intvar()
to force each value to be an integer before aggregating them and running
mktime()
with the same result.

(Man ... how short is the editing window on this forum?!)

jatar_k

10:10 pm on May 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



wrong number of params, that's why

everything passed to mktime is optional, so...

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

when you call this
mktime(00,01,12,$thisdate)

that is only 4 params so you are passing 01,24,2005 as the month, since that isn't a proper format for month mktime does now, as it does when it gets bad params or we get that fancy dat e from 1969.

<added>editing is pretty short, well I can only gues, mine is infinite in this forum ;)

StupidScript

10:19 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I ended up doing (where $logentry[1] is the date/time in an Apache log):

$thisrec=explode(":",$logentry[1]);

$thisd=explode("/",$thisrec[0]);

switch ($thisd[1]) {

case "Jan": $thismo="01";break;

case "Feb": $thismo="02";break;

etc.
}

$thisdate=mktime($thisrec[1],$thisrec[2],$thisrec[3],$thisd[0],$thismo,$thisd[2]);

to get the right timestamp, but it seems like such a waste.

Thank you for your response, jatar_k. Because I'm a bit anal in this way, would you give an explanation for why $thisdate in my first example isn't being parsed by mktime(), but is instead being treated as a single parameter despite the included commas?

jatar_k

10:25 pm on May 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> treated as a single parameter

because that's what it is, not very technical but it is really just a syntax question more than anything else

if a function is expecting 2 values as parameters you can't do

$myparams = 'val1,val2';
$var1 = somefunction($myparams);

it will die with a 'wrong number of parameters on line x' or something like that. The only reason it seemd to work for mktime is that all of the params are optional so it didn't kill itself.

If php was going to parse through the contents of every var all the time it would die screaming in an infinite loop or maybe the server would just burst into flames. :)

StupidScript

10:27 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



burst into flames

Coool!

It is interesting that mktime() got the month and year correct when passing the single parameter, just not the day. (Like passing 00,01,12,06, ,2005)

Thanks for your help.

jatar_k

10:33 pm on May 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



year is the same as this year

and month is the first param and it probably read it up to the first comma

StupidScript

10:36 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bingo. Sounds good. Thanks.

coopster

11:51 am on May 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, I remember playing around with the same thing once, StupidScript ... I ended up using a list ...

[webmasterworld.com...]