Jump to content

Date sorting not working in my html code


Go to solution Solved by Barand,

Recommended Posts

I am trying to sort my dates, but it shows it like this when I click the header.
Does not matter what JavaScript I use, al of them that I tried, show it like the first part,
Is there in my php code to get it to work.  Need the last td to be ascending or descending 

0 Jare -- 0 Maande -- 10 Dae

0 Jare -- 0 Maande -- 11 Dae

0 Jare -- 0 Maande -- 13 Dae

0 Jare -- 0 Maande -- 2 Dae

0 Jare -- 3 Maande -- 2 Dae

0 Jare -- 0 Maande -- 25 Dae

0 Jare -- 0 Maande -- 3 Dae

instead of 

0 Jare -- 0 Maande -- 2 Dae

0 Jare -- 0 Maande -- 3 Dae

0 Jare -- 0 Maande -- 10 Dae

0 Jare -- 0 Maande -- 11 Dae

0 Jare -- 0 Maande -- 13 Dae

0 Jare -- 0 Maande -- 25 Dae

 

<td>
	<?php
		$date = $checkSqlRow["CREATED_AT"];
		$newDate = date("Y M d", strtotime($date));
		echo $newDate;
	?>
</td>
										
<td>
	<?php
		$date1 = $date = $checkSqlRow["CREATED_AT"];
		$date2 = date("y-m-d"); // this returns today's date
		$diff = abs(strtotime($date2) - strtotime($date1));
		$years = floor($diff / (365 * 60 * 60 * 24));
		$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
		$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
			echo $years . " Jare -- \n";
			echo $months . " Maande -- \n";
			echo $days . " Dae\n";
	?>
</td>

 

those values are strings. they will be sorted according to the character order at each position in the string. a '1' at any position in the string, regardless of it being part of '1', '10', '1000000', is less then a '2' at that same position. you can get this to working by padding the values with leading zeros, so that each numerical field in the string is the same length.

Or do it the easy way

code
 

$res = $pdo->query("SELECT created_at
                    FROM test_b
                    ORDER BY created_at DESC
                    ");

$today = new DateTime('now');
      
foreach ($res as $row)  {
    $dt = new DateTime($row['created_at']);
    $diff = $dt->diff($today)->format("%y years -- %m months -- %d days");
    echo "{$row['created_at']} | $diff <br>";
}

output

2020-10-15 00:00:00 | 1 years -- 9 months -- 22 days 
2020-09-06 00:00:00 | 1 years -- 11 months -- 0 days 
2019-04-09 00:00:00 | 3 years -- 3 months -- 28 days 
2019-03-08 00:00:00 | 3 years -- 4 months -- 29 days 
2018-06-06 00:00:00 | 4 years -- 2 months -- 0 days 
2018-06-04 00:00:00 | 4 years -- 2 months -- 2 days 
2018-05-27 00:00:00 | 4 years -- 2 months -- 10 days 
2018-05-15 00:00:00 | 4 years -- 2 months -- 22 days 
2018-02-03 00:00:00 | 4 years -- 6 months -- 3 days 
2018-01-05 00:00:00 | 4 years -- 7 months -- 1 days 

 

  • Like 1
1 hour ago, Barand said:
$res = $pdo->query("SELECT created_at
                    FROM test_b
                    ORDER BY created_at DESC
                    ");

$today = new DateTime('now');
      
foreach ($res as $row)  {
    $dt = new DateTime($row['created_at']);
    $diff = $dt->diff($today)->format("%y years -- %m months -- %d days");
    echo "{$row['created_at']} | $diff <br>";
}

Good day Barand

Thank you, I dis replace my code (my second part) wit the code you have giving me, But it is giving me the following error

Fatal error: Uncaught Error: Call to a member function query() on null in C:\Mylinks\Kripto\Crypto\index.php:478 Stack trace: #0 {main} thrown in C:\Mylinks\Kripto\Crypto\index.php on line 478

 

<?php
				$res = $pdo->query("SELECT created_at FROM cmc_my_munte ORDER BY created_at DESC ");
				$today = new DateTime('now');
			  
					foreach ($res as $row)  
						{
							$dt = new DateTime($row['created_at']);
							$diff = $dt->diff($today)->format("%y years -- %m months -- %d days");
							echo "{$row['created_at']} | $diff <br>";
						}
          ?>

Will this work if I have three different tables,  cmc(coinmarkcetcap), cgo(coingeko) and nmc (nomics)

57 minutes ago, hendrikbez said:

Fatal error: Uncaught Error: Call to a member function query() on null

Because you don't have database connection in a variable $pdo.

58 minutes ago, hendrikbez said:

Will this work if I have three different tables,  cmc(coinmarkcetcap), cgo(coingeko) and nmc (nomics)

That depends on what you do with them.

1 hour ago, Barand said:

Because you don't have database connection in a variable $pdo.

Thank you, I still learning php, I did add the following to my connection code, but think it wrong, from line 16.

Forgive me for this stupid question

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "kripto";
$port = "3306";
$charset = 'utf8mb4';
// Create connection
$conn= mysqli_connect($servername,$username,$password,$dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected Successfully.";

$options =
	[
		\PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
		\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
		\PDO::ATTR_EMULATE_PREPARES   => false,
	];
		$dsn = "mysql:host=$localhost;dbname=$dbname;charset=$charset;port=$port";
			try {
				$pdo = new \PDO($dsn, $user, $pass, $options);
		} catch (\PDOException $e) {
		throw new \PDOException($e->getMessage(), (int)$e->getCode());
	}

?>

 

 

10 hours ago, Barand said:

All you needed to do was substitute your connection "$conn" for my "$pdo". My coding in this instance is mysqli/pdo neutral.

Hi Barand

Now I have learned something new, like you have said, just change $conn to $pdo.  I did change all the $conn on all of my files tp $pdo and it is working.

Now to go and search what is the difference between $conn and $pdo.

Thanks ones again.

 

 

11 hours ago, Barand said:

All you needed to do was substitute your connection "$conn" for my "$pdo". My coding in this instance is mysqli/pdo neutral.

I am getting this error on this line now( did try to take out {}, but then give other error, so I put it back. 

Warning: Invalid argument supplied for foreach() in C:\Mylinks\Kripto\Crypto\index.php on line 480

foreach ($res as $row){$dt = new DateTime($row['created_at']);

 

Then remove it. I put it there to make it easy for me to check that the data differences were correct.

When you come across an example of how to do something then you have to adapt that example to your situation and needs - not blindly copy, paste and pray.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.