954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Breaking strings

First Issue: Problem
So on a forum, i've condensed the date of a certain event to

$date = $month . " " . $day . ", " . $year;

So instead of storing the Month Day and Year in seperate rows on mysql, i store all three under 1.

But when I want to take this information that i've stored and break it back into 3 parts, how do I do it?

ex) $date = October 14, 2006

I want it seperate out to $month=october $day=14 and $year=2006...

Second Issue: Opinion
Did I make this too complicated? Is there an easier way to store selected dates in a database and retrieving them for updating? (I don't want to store "current" date.

vexhawk
Newbie Poster
24 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

First Issue:
You can use:

list($month,$day,$year)=preg_split("/[,\s]/",$date); // (untested!)


or you can use the strtotime() function, which will convert it to a php datetime, and then do whatever you need to (maybe using date() to reformat or whatever)


Second Issue:
MySQL has a datetime format, which is probably the best to use. Then, if you use MySQL's UNIX_TIMESTAMP function when selecting rows, it'll automatically be in PHP's format...

For more information, there are tons of tutorials on the net, such as this one (not that I've used it, but a quick search yielded this) http://www.devshed.com/c/a/MySQL/Practical-Date-Time-examples-with-PHP-and-MySQL/

MCP
Light Poster
44 posts since Oct 2006
Reputation Points: 14
Solved Threads: 3
 

You can use substr(strings, begin, length) to separate the strings. Here are the exampe:

Assume that date store in database is in this format: 2005-02-10

Retrieve the date from the database and store into a variable $date.

$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);

Note: You must ensure the length and format of your date are same. Otherwise it will retrieve the different value under the same location.

rinoa04
Junior Poster in Training
84 posts since Sep 2006
Reputation Points: 52
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You