Jump to content

help me with mktime please


tomasd

Recommended Posts

Hi,

I'm not sure what am I doing wrong here...

<?php
$start_day = "01";
$start_month = "01";
$start_year = "2007";
$end_day = "10";
$end_month = "01";
$end_year = "2007";

$start_date = date('Ymd', mktime(0, 0, 0, $start_month, $start_day, $start_year));
$end_date = date('Ymd', mktime(0, 0, 0, $end_month, $end_day, $end_year));

for ($i = $start_date; $i < $end_date; $i++)
{
echo "$i \n";
$newdate = date('c', $i);
echo "$newdate \n";
}
?>

I was expecting to see $i incremented by day for both $i and $newdate but I'm getting following instead...

 

20070101

1970-08-21T07:01:41+00:00

20070102

1970-08-21T07:01:42+00:00

20070103

1970-08-21T07:01:43+00:00

20070104

1970-08-21T07:01:44+00:00

20070105

1970-08-21T07:01:45+00:00

20070106

1970-08-21T07:01:46+00:00

20070107

1970-08-21T07:01:47+00:00

20070108

1970-08-21T07:01:48+00:00

20070109

1970-08-21T07:01:49+00:00

 

What am I doing wrong?

 

Any help appreciated!

 

Link to comment
https://forums.phpfreaks.com/topic/94295-help-me-with-mktime-please/
Share on other sites

This would be much easier

 

$start = mktime(0, 0, 0, $start_month, $start_day, $start_year);

$end = mktime(0, 0, 0, $end_month, $end_day, $end_year);

 

for ($i = $start; $i <= $end; $i += 86400) {

  echo "$i \n";

  $newdate = date('c', $i);

  echo "$newdate \n";

}

 

86400 is the amount of seconds in a day (60 * 60 * 24)

mktime() takes integer values. "01" might be messing it up. quoted strings might be messing it up, but i doubt it. probably the 01, it's not an integer.

 

PHP isn't strict with variable types. A numeric string can be interpreted as an int/float/double ect... His problem was trying to call date with a 'yyyymmdd' formatted string, and not a timestamp.

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.