Jump to content

making string a database record


x_maras

Recommended Posts

Hi,

 

I use 2 functions for finding what day is in a specific date.

The first one returns a number from 1 to 7 for the days of the week.

 

I found this function:

<?php
function getDayOfWeek($day, $month, $year, $calendarSystem = 1){
    # Check for valid parameters #
    if (!is_int($day) || $day < 1 || $day > 31){
        printf('Wrong parameter for $day. It must be an integer between 1 and 31.');
        exit();
    }
       
    if (!is_int($month) || $month < 1 || $month > 12){
        printf('Wrong parameter for $month. It must be an integer between 1 and 12.');
        exit();
    }
       
    if (!is_int($year) || $year < 0){
        printf('Wrong parameter for $year. It must be a positive integer.');
        exit();
    }
       
    if (!checkdate($month, $day, $year)){
        printf('Invalid date.');
        exit();
    }
       
    # Algorithm #       
    if ($month < 3){
        $month = $month + 12;
        $year = $year - 1;
    }
       
    return ($day + (2 * $month) + (int) (6 * ($month + 1) / 10) + $year + (int) ($year / 4) - (int) ($year / 100) + (int) ($year / 400) + $calendarSystem) % 7;
}

?>

 

and the second takes a string with the date as "09-07-2009" and

first feeds the first one with the parameters day, month and year and

second matches the number with a day.

 

I made this:

<?php
function showDay($str) {
	$arr = str_split($str);
	if($arr[0] != '0'){
		$dag = $arr[0] . $arr[1];
	} else {
		$dag = $arr[1];
	}

	if($arr[3] != '0'){
		$maand = $arr[3] . $arr[4];
	} else {
		$maand = $arr[4];
	}

	$jaar = $arr[6] . $arr[7] . $arr[8] . $arr[9];

	$dag = intval($dag);
	$maand = intval($maand);
	$jaar = intval($jaar);

	$day = getDayOfWeek($dag, $maand, $jaar);
	switch($day){
		case 1: 
			$day = "Maandag";
			break;
		case 2: 
			$day = "Dinsdag";
			break;
		case 3: 
			$day = "Woensdag";
			break;
		case 4: 
			$day = "Donderdag";
			break;
		case 5: 
			$day = "Vrijdag";
			break;
		case 6: 
			$day = "Zaterdag";
			break;
		case 7: 
			$day = "Zondag";
			break;
	}
	return $day;
}
?>

 

I tried these but I had different results:

 

<?php
include("database.php");

$sql = "SELECT * FROM course";
$res = mysql_query($sql);
$result = mysql_fetch_array($res);

echo showDay($result['c_date']);

$mera = "10-09-2009";
$something = showDay($mera);
echo $something;
?>

 

The first echo prints 0

and the second one prints the Thursday!

 

Does anyone know why this is happening?

I try to use strval() but I had the same result, I also tried casting but nothing.

 

Can someone help me with this?

 

Thank you in advance,

Dinos

 

Link to comment
https://forums.phpfreaks.com/topic/165335-making-string-a-database-record/
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.