...

View Full Version : formatting a date string?



thesavior
01-02-2006, 06:13 PM
How can i format a string like this: 20060102095848

into something like:

January 2nd, 2006

I don't need the time part of that, but what im not sure is how to split it up reliably for all different strings of the same type.

thesavior
01-02-2006, 06:32 PM
*added information*

Im using now()

Is there another code that will format it the way i want?

ralph l mayo
01-02-2006, 06:44 PM
strtotime() (http://us3.php.net/strtotime)
date() (http://us3.php.net/date)
substr() (http://us3.php.net/substr)

thesavior
01-02-2006, 06:46 PM
ok, im getting mysql errors saying my syntax is wrong on this. Im not sure why it is though.


$query1 = "INSERT INTO tutorials (date,image,title,description,link,category,author) VALUES (date('F jS, Y'),'".$image."','".$title."','".$description."','".$link."','".$category."','".$author."')";
mysql_query($query1)or die(mysql_error());
?>

ralph l mayo
01-02-2006, 06:48 PM
ok, im getting mysql errors saying my syntax is wrong on this. Im not sure why it is though.


$query1 = "INSERT INTO tutorials (date,image,title,description,link,category,author) VALUES (date('F jS, Y'),'".$image."','".$title."','".$description."','".$link."','".$category."','".$author."')";
mysql_query($query1)or die(mysql_error());
?>

date('F jS, Y') is in quotation marks, but it's not a MySQL function, it's a PHP function.

thesavior
01-02-2006, 06:52 PM
ok, so then how can i do this with mysql then?

ralph l mayo
01-02-2006, 07:03 PM
errr, don't.



$query1 = "INSERT INTO tutorials (date,image,title,description,link,category,author) VALUES ('".date('F jS, Y')."','".$image."','".$title."','".$description."','".$link."','".$category."','".$author."')";

Velox Letum
01-02-2006, 07:05 PM
See DATE_FORMAT() on MySQL data and time functions (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html).

Ralph's works just fine, but when selecting data DATE_FORMAT() would be useful. You could use this anyways, but the PHP solution works just as well.

thesavior
01-02-2006, 07:15 PM
his didn't work, im getting an error:



$query1 = "INSERT INTO tutorials (date,image,title,description,link,category,author) VALUES ('".date('F jS, Y')."','".$image."','".$title."','".$description."','".$link."','".$category."','".$author."')";



Parse error: parse error, unexpected T_VARIABLE in c:\phpdev5\www\tms\processtut.php on line 13

ralph l mayo
01-02-2006, 07:28 PM
his didn't work, im getting an error:



$query1 = "INSERT INTO tutorials (date,image,title,description,link,category,author) VALUES ('".date('F jS, Y')."','".$image."','".$title."','".$description."','".$link."','".$category."','".$author."')";



Parse error: parse error, unexpected T_VARIABLE in c:\phpdev5\www\tms\processtut.php on line 13

Are you missing a semicolon on the line preceeding this?

thesavior
01-02-2006, 07:30 PM
...no... :rolleyes and look innocent:

lol
:p :thumbsup:

thesavior
01-02-2006, 07:51 PM
what does this error mean, and how do i fix it?


You have an error in your SQL syntax near 'desc) VALUES ('January 2nd, 2006','aj;lkajfl;kjads;fkj','flk;lajd;lfkj;lasdjf','' at line 1


<?php
// set your information.
$dbhost='localhost';
$dbusername='root';
$dbuserpass='';
$dbname='test';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);

if (!mysql_select_db($dbname)) die(mysql_error());
$now = date('F jS, Y');
$query1 = "INSERT INTO tutorials (date,image,title,tuturl,category,author,desc) VALUES ('".date('F jS, Y')."','".$image."','".$title."','".$url."','".$category."','".$author."','".$desc."')";
mysql_query($query1)or die(mysql_error());
?>

ralph l mayo
01-02-2006, 09:06 PM
I don't have mysql right now to check but my guess is it's because DESC is an SQL keyword meaning DESCending in the ORDER BY clause. Change the column name or try putting it in quotes or something and see if you can't get it working.

firepages
01-03-2006, 02:05 AM
what is the MySQL type of the 'date' field ? (and is that also a reserved word in MySQL in which case use `date` (with the backticks) in fact use backticks on all the fieldnames anyway `desc` etc)

In your initial post you showed a MySQL timestamp(14) , if thats the type of the date field then thats the sort of input its expecting.

thesavior
01-03-2006, 02:10 AM
oh sorry, i forgot to post a "IT WORKS!! W00t!!! post"

It works now, i just changed desc to descr

Thanks guys for all your help and quick replies.

Velox Letum
01-03-2006, 02:17 AM
what is the MySQL type of the 'date' field ? (and is that also a reserved word in MySQL in which case use `date` (with the backticks) in fact use backticks on all the fieldnames anyway `desc` etc)

In your initial post you showed a MySQL timestamp(14) , if thats the type of the date field then thats the sort of input its expecting.

I wonder too...DATE type is the date stored in YYYY-MM-DD form, but as it isn't part of a query I don't think its reserved like DESC. TIMESTAMP is a quirky column type that is displayed in DATETIME form, but changes after MySQL 4.1 and I believe it automatically updates. Correct me if I'm wrong on timestamp fields, I never use them. I always use DATETIME or INT(11). I recommend just using unix timestamps with INT(11).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum