Jump to content

Problems with date()


Psyborg

Recommended Posts

I'm trying to switch two dates if one is before the other, but I'm getting some very strange output from this code....

 

 

I've verified that the variables contain the correct information, HOWEVER when this code is run, the returns are:

 

$rangeStart = 2016-03-02  <- This is the return even if I give it a static date!

$rangeEnd = 2010-09-19

 

or blank!  What am I doing wrong?

 

 

For this example:

 

$selDay = 17;
$selMonth = 9;
$selYear = 2010;
$pDay[0] = 14; //Day
$pDay[1] = 9; //Month
$pDay[2] = 2010; //Year

$errMsg = "selDay: ".$selDay."-".$selMonth."-".$selYear."; pDay: ".$pDay[0]."-".$pDay[1]."-".$pDay[2]."; selRange: ".$selRange."; TEMP: ";

        if(mktime(1,1,1,$selMonth,$selDay,$selYear)>mktime(1,1,1,$pDay[1],$pDay[0],$pDay[2]))
{
	$reverse = true;

	$tmp = $pDay[0];
	$pDay[0] = $selDay;
	$selDay = $tmp;
	$errMsg.="<br />selDay=".$selDay."<br />pDay[0]=".$pDay[0];


	$tmp = $pDay[1];
	$pDay[1] = $selMonth;
	$selDay = $tmp;
	$errMsg.="<br />selMonth=".$selMonth."<br />pDay[1]=".$pDay[1];

	$tmp = $pDay[2];
	$pDay[2] = $selYear;
	$selDay = $tmp;
	$errMsg.="<br />selYear=".$selYear."<br />pDay[2]=".$pDay[2];
}
$RangeStart = date("Y-m-d",mktime(1,1,1,$selMonth,$selDay,$selYear)); //Selected Range start in d-m-Y
$RangeEnd = date("Y-m-d",mktime(1,1,1,$pDay[1],$pDay[0],$pDay[2])); //Selected Range start in d-m-Y
$errMsg.=";<br /> Range Start: ".$pRangeStart."; <br />Range End: ".$pRangeEnd.";

echo $errMsg;

 

Returns:

 

selDay: 17-9-2010; 
pDay: 14-9-2010; 
selRange: ;
TEMP: 
selDay=14
pDay[0]=17
selMonth=9
pDay[1]=9
selYear=2010
pDay[2]=2010;
Range Start: ; 
Range End: ;

 

This is getting annoying.  Any help would be wonderful.

Link to comment
https://forums.phpfreaks.com/topic/214019-problems-with-date/
Share on other sites

if all you are essentially doing is comparing the two dates...

 

Psuedo code

1. variables

      startdate

      enddate

 

2. convert startdate and enddate to timestamps (startdate_ts and enddate_ts)

 

3. if startdate_ts<enddate_ts

    do nothing

    else

    holdate = endate

    endate = startdate

    startdate = holddate

    endif

 

Link to comment
https://forums.phpfreaks.com/topic/214019-problems-with-date/#findComment-1113784
Share on other sites

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.