Click here to Skip to main content
15,564,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not very good in php and javacript, need help on how to fix the next info,
Need to show total days in html table.

I have the first date in mysql table [CREATED_AT] and then todays date (not in database). Only what to show the total days in my html table, when I open the webpage.

Did get the script code from searching , but it does not show the info at all.

Not sure how to use print in the script

How do I get it to work

Edit: Further information that OP posted in a solution. Putting it here before it is deleted
it is giving error in

echo ("%d days\n", $days);
.
So when I take out that line then it the give me this error.

Warning: Use of undefined constant today - assumed 'today' (this will throw an Error in a future version of PHP) in C:\Mylinks\Kripto\Crypto\index.php on line 694


I do not have "today;" in my database. I just want it to get date from system date for it.

What I have tried:

<pre lang="PHP"><pre><?php $date = $checkSqlRow["DAYS_OLD"]; ?></td>
<script type="text/javascript">
$date1 = $date = $checkSqlRow["CREATED_AT"];
$date2 = today;
$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 ("%d days\n", $days);

</script>
Posted
Updated 21-Jul-22 3:40am
v3
Comments
Richard MacCutchan 21-Jul-22 9:45am    
See my updated solution.

1 solution

The code you are trying to use is PHP not Javascript. Try this:
PHP
<?php $date = $checkSqlRow["DAYS_OLD"]; ?></td>
<?php
$date1 = $date = $checkSqlRow["CREATED_AT"];
$date2 = today;
$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 ("%d days\n", $days);
?>


This works (assuming $date1 is valid):
[edit]
PHP
$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 . " years\n";
echo $months . " months\n";
echo $days . " days\n";

[/edit]
 
Share this answer
 
v3
Comments
CHill60 21-Jul-22 8:41am    
OP is responding via the medium of a "Solution" - I've update their post with their extra info and advised them to delete the solution
Richard MacCutchan 21-Jul-22 9:46am    
Thanks.
Odiez Bez 21-Jul-22 13:44pm    
Thank you for your help, It is working.
Odiez Bez 21-Jul-22 12:23pm    
@Richardmaccutchan

Thank you for your help, It is working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900