date format not working Topic is solved

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
rwahdan
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Thu Jul 05, 2018 8:12 am

date format not working

Post by rwahdan » Mon Jul 23, 2018 5:10 am

Hi,
I am getting date from database table. in the loop i am trying to change the format of date before it is displayed but it is not working, the results are blank, before i tried to format i get the results of the database table.

Here is the code:

Code: Select all

foreach($results as $row){

	$thedate= $row->dateviewed ;
	$thedate = date_format($thedate,'d/m/Y H:i:s');

	echo "<tr>"; 
	echo "<td>".$row->username."</td>";
	echo "<td>".$row->articlename."</td>";
       	echo "<td>".$thedate."</td>";
	echo "</tr>"; 
}

If i change the last <td> of code to original database date

Code: Select all

echo "<td>".$row->dateviewed."</td>";
It will work. Please help me on this.

Thanks
You do not have the required permissions to view the files attached to this post.
Last edited by imanickam on Mon Jul 23, 2018 9:11 am, edited 1 time in total.
Reason: Moved the topic from the forum Administration Joomla! 3.x to the forum Joomla! 3.x Coding

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: date format not working

Post by SharkyKZ » Mon Jul 23, 2018 11:28 am

The input for date_format() must be DateTime object, not string. You can create the object with date_create():

Code: Select all

$thedate = date_create($row->dateviewed);
$thedate = date_format($thedate,'d/m/Y H:i:s');
echo $thedate;
You can also use Joomla's Date API:

Code: Select all

$thedate = new JDate($row->dateviewed);
echo $thedate->format('d/m/Y H:i:s');

rwahdan
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Thu Jul 05, 2018 8:12 am

Dates mysql and php

Post by rwahdan » Mon Jul 23, 2018 2:46 pm

Hi,

I have a problem working with dates. I have a table in mysql that has field with Date/Time datatype and would like to get the data from the table to display in my php page as following:

23-7-2018 6:45:00 PM noting that i am in +4GMT (Asia/Dubai) zone.
the field in the database is represented as 2018-7-23 14:45:00 GMT only

Note: I want to save the new date format to variable say $newdate then will use that to put in table in my page.

Thanks.

User avatar
donnan
Joomla! Ace
Joomla! Ace
Posts: 1494
Joined: Sat Jun 30, 2007 1:23 am
Location: Sydney, Australia
Contact:

Re: Dates mysql and php

Post by donnan » Tue Jul 24, 2018 1:18 am

Dates have variables assigned to them eg;

$Y = year
$m = month
$d = day

$h = hour
$m = minutes
$s = seconds

So you can use those variables to create how you wish to display the date.

Hope this helps.

rwahdan
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Thu Jul 05, 2018 8:12 am

Re: Dates mysql and php

Post by rwahdan » Tue Jul 24, 2018 11:44 am

donnan wrote:
Tue Jul 24, 2018 1:18 am
Dates have variables assigned to them eg;

$Y = year
$m = month
$d = day

$h = hour
$m = minutes
$s = seconds

So you can use those variables to create how you wish to display the date.

Hope this helps.
Here is what i did, but instead of getting the value from the DB table, it is getting the current time! If i refresh the page the time changes to current time, so it is not displaying the DB field.

Code: Select all

date_default_timezone_set('Asia/Dubai'); 

echo "<table><tr><th>User Name</th><th>Article Name</th><th>Accessed On</th></tr>";

foreach($results as $row){

    
    $thedate = $row->dateviewed;
    $thedate = date('d-[URL banned] h:i:s A');
    echo $thedate;
echo "<tr>"; 
echo "<td>".$row->username."</td>";
echo "<td>".$row->articlename."</td>";
       echo "<td>" .$thedate. "</td>";
echo "</tr>"; 

}
Please help.

User avatar
donnan
Joomla! Ace
Joomla! Ace
Posts: 1494
Joined: Sat Jun 30, 2007 1:23 am
Location: Sydney, Australia
Contact:

Re: date format not working

Post by donnan » Tue Jul 24, 2018 9:05 pm

this is what you need.

Code: Select all

$thedate = new JDate($row->dateviewed);
echo $thedate->format('d/m/Y H:i:s');


Locked

Return to “Joomla! 3.x Coding”