Hi Guys Im a newbie and a somewhat beginner in PHP
Here is my problem

I want to insert this date format - DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news


into this already existing sql statement - $sql = "SELECT * FROM authors aut, news new WHERE aut.aurthor_id=new.arthor_id AND article_id=1 ORDER BY updated DESC LIMIT 15";


I have tried all sorts of way but keep getting this (or similar) error message - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(news.updated, '%%M %%e, %%Y') AS formatted from news WHERE aut.aurthor_id=new.a' at line 1

Member Avatar for diafol

Perhpas you want to check your spelling too:

authors aut, news new WHERE aut.aurthor_id=new.arthor_id

They may be right in your case, but 3 different ways of spelling author?

Your question doesn't make sense...I think you have a field with a timestamp and you want it to be displayed as a normal date in PHP. Is that what you mean. I don't know how to do it but I'm sure ARDAV can help you.

The link that StephNicolaou provided should help you out. You can also look at http://www.tizag.com/phpT/phpdate.php which gives much of the same information but Tizag is a good beginners site and they tend to explain things in as simplified way as they can.

PHP has the built-in function 'date' which gets the timestamp from the server and within the parenthesis of the date function you can specify how you want the date to be displayed.

SO you could do something like:

<?php
$date_time = date("m/d/y");
?>

Then you have the date and time in the format that you want it displayed and can send the variable with your query to have it saved in the database.

Now at the time of this posting the value of $date_time would be "03/13/12" which doesn't show the name of the day of the week or the time but you can use either of the function links we've given you to format it how you want.

To test the format you can make your script print out the value of $date_time without saving it to the database so you can see how the format would actually look. And then you can change the parameters of 'date' to get it to look how you want.

echo date('d-m-Y', strtotime($date));

for change date format in php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.