We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,382 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Date Format

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

6
Contributors
5
Replies
19 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
mtho
Newbie Poster
16 posts since Mar 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Modify the double percentage symbols to a single percentage symbol, e.g. '%M %e, %Y'

Double percentages are a literal % symbol.

Here is a MySQL guide to every format available:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

Hope that helps :)

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
Skill Endorsements: 0

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?

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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.

Que336
Light Poster
26 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

weekendrockstar
Junior Poster in Training
71 posts since Dec 2010
Reputation Points: 25
Solved Threads: 16
Skill Endorsements: 0

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

for change date format in php

rajeevphp2011
Light Poster
26 posts since Nov 2011
Reputation Points: 7
Solved Threads: 6
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1254 seconds using 2.7MB