John Davidson

php - Why foreach not working with date changer?

0 comments
Message:


I want to change my database dates from jalali to gregorian. but when I run the code, foreach not working and not getting all of my datas from database.
This is my code in my php file:


$sqlslct = "SELECT * FROM numberslist1";
$resslct = $connect->query($sqlslct);
foreach($resslct as $rsslct) {
$datee = $rsslct['datee'];
$dateeex = explode("-", $datee);
function div($a,$b) {
return (int) ($a / $b);
}
function jalali_to_gregorian($j_y, $j_m, $j_d,$str) {

$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);


$jy = (int)($j_y)-979;
$jm = (int)($j_m)-1;
$jd = (int)($j_d)-1;

$j_day_no = 365*$jy + div($jy, 33)*8 + div($jy%33+3, 4);

for ($i=0; $i < $jm; ++$i)
$j_day_no += $j_days_in_month[$i];

$j_day_no += $jd;

$g_day_no = $j_day_no+79;

$gy = 1600 + 400*div($g_day_no, 146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */
$g_day_no = $g_day_no % 146097;

$leap = true;
if ($g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */
{
$g_day_no--;
$gy += 100*div($g_day_no, 36524); /* 36524 = 365*100 + 100/4 - 100/100 */
$g_day_no = $g_day_no % 36524;

if ($g_day_no >= 365)
$g_day_no++;
else
$leap = false;
}

$gy += 4*div($g_day_no, 1461); /* 1461 = 365*4 + 4/4 */
$g_day_no %= 1461;

if ($g_day_no >= 366) {
$leap = false;

$g_day_no--;
$gy += div($g_day_no, 365);
$g_day_no = $g_day_no % 365;
}

for ($i = 0; $g_day_no >= $g_days_in_month[$i] + ($i == 1 && $leap); $i++)
$g_day_no -= $g_days_in_month[$i] + ($i == 1 && $leap);
$gm = $i+1;
$gd = $g_day_no+1;
if($str) return $gy.'/'.$gm.'/'.$gd ;
return array($gy, $gm, $gd);
}
$str = true;
$datee2 = jalali_to_gregorian($dateeex[0], $dateeex[1], $dateeex[2],$str);
$datee1 = explode("/", $datee2);
$datee4 = jalali_to_gregorian($dateeex[0], $dateeex[1], $dateeex[2],$str);
$datee3 = str_replace("/", "-", $datee4);
$month = $datee1[1];
$year = $datee1[0];
echo $datee3." / ".$month." / ".$year."<br>";
}

This code should print a tall result of dates from my database in the page, like this example:


2022-2-9 / 2 / 2022
2022-2-10 / 2 / 2022
2022-2-11 / 2 / 2022
2022-2-12 / 2 / 2022
2022-2-13 / 2 / 2022
2022-2-14 / 2 / 2022
And more...

But it will show just one date in the page. why is this happen? is there any problem with date changer? because I tested the code without jalali_to_gregorian() function and the result was correct.



My Answer:
"SELECT * FROM my_table";
$result = $conn->query($sqlslct);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$jalali_date = $row['date'];
$gregorian_date = convert_jalali_to_gregorian($jalali_date);

$sqlupdate = "UPDATE my_table SET date='$gregorian_date' WHERE id=" . $row['id'];
$conn->query($sqlupdate);
}
} else {
echo "0 results";
}

function convert_jalali_to_gregorian($jalali_date) {
// Your conversion logic here
}

Make sure that your convert_jalali_to_gregorian function is correctly implemented and returns the converted date in the correct format. Also, ensure that your database connection is properly established and that your SQL queries are correct. If the foreach loop is not working as expected, you may need to debug your code to identify any potential issues.

Rate this post

5 of 5 based on 7344 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us