Getting Certain Parts Of A Record - The character data

Pages: 1, 2
free web hosting

Read Latest Entries..: (Post #17) by pyost on Oct 27 2007, 08:12 AM. (Line Breaks Removed)
QUOTE(FirefoxRocks @ Oct 26 2007, 02:43 PM) There are date and time functions here on dev.mysql.com, exact url is http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html.Heh, didn't think of checking MySQL functions
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Getting Certain Parts Of A Record - The character data

FirefoxRocks
Ok I need help on this puzzling problem. At first I thought that this person stored the dates in the MySQL database like this:

August 27, 2007

That kinda freaked me out a little, because string dates are hard to manipulate. Then I found out that he stored both th string data and numerical date, which I found a little bit odd, but it was like this:

2007-08-27

I need to build a PHP program to manipulate the data, but I need to access the year, month and day respectively by themselves.

I think that isolating the first 4 characters for the year, last 2 characters for day and 6th and 7th characters for month would be the easiest method to do this. Then I could pass the data onto an XML file, in then which I will use XSLT to transform the data. So how do I do that? (I already got the XML/XSLT part, but I need help on the initial problem.)

Reply

TavoxPeru
QUOTE(FirefoxRocks @ Oct 16 2007, 05:09 PM) *
Ok I need help on this puzzling problem. At first I thought that this person stored the dates in the MySQL database like this:

August 27, 2007

That kinda freaked me out a little, because string dates are hard to manipulate. Then I found out that he stored both th string data and numerical date, which I found a little bit odd, but it was like this:

2007-08-27

I need to build a PHP program to manipulate the data, but I need to access the year, month and day respectively by themselves.

I think that isolating the first 4 characters for the year, last 2 characters for day and 6th and 7th characters for month would be the easiest method to do this. Then I could pass the data onto an XML file, in then which I will use XSLT to transform the data. So how do I do that? (I already got the XML/XSLT part, but I need help on the initial problem.)

To do this you can use the substr() php function or directly using the MySql DATE_FORMAT() function:

CODE
<?php
// using DATE_FORMAT or substr
$sql="SELECT DATE_FORMAT(table.col_date,'%d') as day, DATE_FORMAT(table.col_date,'%M') as month, DATE_FORMAT(table.col_date,'%Y') as year, table.col_date as thedate from table";
$rs = mysql_query($sql) or die($sql." : ".mysql_error());
$row = mysql_fetch_array($rs);
$day=$row["day"];
$month=$row["month"];
$year=$row["year"];

// using substr
$year1=substr($row["thedate"],0,4);
$month1=substr($row["thedate"],5,2);
$day1=substr($row["thedate"],8,2);
?>


Best regards,

 

 

 


Reply

kelvinmaki
QUOTE
Ok I need help on this puzzling problem. At first I thought that this person stored the dates in the MySQL database like this:

August 27, 2007

That kinda freaked me out a little, because string dates are hard to manipulate. Then I found out that he stored both th string data and numerical date, which I found a little bit odd, but it was like this:

2007-08-27

I need to build a PHP program to manipulate the data, but I need to access the year, month and day respectively by themselves.


Anyway for what I know, selecting the date from mysql database will ALWAYS be in YYYY-MM-DD, eg. 2007-10-17. Unless the data type of that date is set to TIMESTAMP. Then it will have YYYY-MM-DD HH:MI:SS, with the time.

Just like to confirm on that. Correct me if I'm wrong. Thanks

Reply

vujsa
Well, I actually wrote a function to manipulate dates quickly some time ago.

http://www.handyphp.com/content/view/10/16/

CODE
// INSERT FUNCTION reformat_date() HERE!

$long_date = "August 27, 2007";

$year = reformat_date($long_date, "Y"); // 2007
$month = reformat_date($long_date, "n"); // 8
$day = reformat_date($long_date, "j"); // 27


I didn't try the YYYY-MM-DD format but I think it will also work. The function is pretty basic and will work for nearly any English time format.


The guy probably used two time formates in his database because he wasn't very good with PHP date / time functions. Whatever the reason, dates aren't really that hard to manipulate if you let PHP do the work for you. You may note that my function is just a combination of two very common PHP date functions. The trick is figuring out the best way to combine PHP functions with your own hard work to get the results you desire.

Personally, I prefer to use UNIX timestamps for all of my date storage. It is easier to compare and manipulate that way.

Good luck,
vujsa

Reply

pyost
You could also use the PHP explode function - it creates an array of elements taken from a string a separated with a delimiter. Or in this case:

CODE
$date = '2007-08-27';

$parts = explode('-', $date);

echo $parts[0]; // 2007
echo $parts[1]; // 08
echo $parts[2]; // 27


Of course, as vujsa said, UNIT timestamps are the best way to store date and time wink.gif

Reply

FirefoxRocks
The dates are stored as YYYY-MM-DD which is a good thing I suppose. At least better than MMM DD, YYYY.

Anyways, I am trying TavoxPeru's method of substr() because it looks simplest. I will test it out sometime today I hope and see if it works as intended. As for explode(), I don't really understand arrays and that kind of stuff, the PHP manual didn't help either. But I will use it if I must. LOL

Reply

pyost
You don't really have to understand arrays, as the following code would give you pure $year, $month and $day string smile.gif

CODE
$date = '2007-08-27';

$parts = explode('-', $date);

$year = $parts[0];
$month = $parts[1];
$day = $parts[2];


Furthermore, if you wanted integers instead of strings, you could convert them easily:

CODE
$yearNum = intval($year);
$monthNum = intval($month);
$dayNum = intval($day);

Reply

Quatrux
to correct pyost a little, due to I think he wrote everything fast without checking and executing the code, the explode function in php parameters are the other way around, I mean like this:

CODE
$parts = explode('-', $date);



Reply

pyost
Thanks Quatrux, I've corrected the two pieces of code wink.gif Though to me it still seems logical for the delimiter to come after the string tongue.gif

Reply

Quatrux
PHP never were a logical language, hahahaha biggrin.gif

Reply

Latest Entries

pyost
QUOTE(FirefoxRocks @ Oct 26 2007, 02:43 PM) *
There are date and time functions here on dev.mysql.com, exact url is http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html.


Heh, didn't think of checking MySQL functions biggrin.gif

Reply

TavoxPeru
QUOTE(pyost @ Oct 26 2007, 03:51 AM) *
There are no such functions in PHP5, or at least according to www.php.net.

That's correct, but you can obtain the month name easily using the strftime(), strtotime(), date() and time() php functions together or directly with the MONTHNAME() MySql function:
CODE
<?php
// php functions
$monthname=strftime("%B",strtotime(date("d-m-Y",time())));

// MySql function
$sql="SELECT MONTHNAME('2007-10-26') as mname";
$rs=mysql_query($sql) or die('Error...');
$row=mysql_fetch_array($rs);
$mname=$row["mname"];

echo "Month Name with Php: " . $monthname;
echo "<br />Month Name with MySql: " . $mname;
?>

Best regards,

Reply

FirefoxRocks
QUOTE(pyost @ Oct 26 2007, 03:51 AM) *
There are no such functions in PHP5, or at least according to www.php.net.


There are date and time functions here on dev.mysql.com, exact url is http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html.

Reply

turbopowerdmaxsteel
I used to get confused by the same very thing. In some of the PHP functions, the order of parameters does defy the common logic. VB & C# support the split function as Split(<StringToSplit>, <Delimeter>). Another such function is str_replace which takes the syntax str_replace(<ReplaceWhat>, <ReplaceWith>, <ReplaceIn>). Again logic says that the source string must be the first parameter as is in, VB & C# - Replace(<ReplaceIn>, <ReplaceWhat>, <ReplaceWith>).

In C#, the Split() and Replace() methods are built into the string object itself. So, the first parameter (Source string) is not specified as the parameter. Rather, these method are called as SourceString.Split() or SourceString.Replace(). But, the thing is same, the source string must be specified before other parameters.

Reply

pyost
There are no such functions in PHP5, or at least according to www.php.net.

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Pages: 1, 2
Recent Queries:-
  1. mysql - 39.02 hr back. (1)
Similar Topics

Keywords : parts, record, character, data

  1. Reading Xml Data
    Within PHP (2)
  2. Letting Users Add Mysql Data With Php
    (1)
    I'm curious as to the best methods of letting users submit data to a MySQL database, displaying
    that data, and removing any unwanted tags etc. from it. Currently, there's a handful of PHP
    functions that I know of to help with this: mysql_real_escape_string() - perhaps the best known
    and most commonly used function, it should be used in pretty much any MySQL query. It escapes
    characters that have SQL significance. QUOTE(php.net) ...which prepends backslashes to the
    following characters: \x00, \n, \r, \, ', " and \x1a I like to think I made a pretty....
  3. Sql Injection Prevention (passing Numerical Data Across Pages).
    PHP/mySQL (9)
    Even if your building something as simple as a basic news page for your website, if your passing
    along url variable strings like (mysite/index.php?page=1), you may be vulnerable to SQL injection
    attacks. For cases like these (passing numerical data in url strings), I have a handy dandy little
    function to thwart these attempts silly: CODE // For checking if value is a number, if not
    return 1. function isNum($val) {   if (!is_numeric($val)) { $val = 1; }   return ($val); } I
    have this function, within my functions.php file, which I use as an include in files w....
  4. Retrieving Data And Displaying In Boxes
    How do I make a grid of boxes? (6)
    I have successfully setup a MySQL Database with a few tables in it to store user input! /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> Woohoo! Now the problem is
    displaying the data. I want 4 boxes echoing across with 3 rows down so that makes 12 boxes (12 of
    the most recent records). I have no clue how to do this because I cannot use the W3Schools example
    of echoing into a table. Here is the code I used but it echoes the same information across th....
  5. Proper Way To Grab User Data?
    (1)
    I'm working on a script where there is a custom user profile and I was wondering if there was a
    more efficient way to grab data stored in a database than this method: CODE $sql = "SELECT *
    FROM users WHERE `access_name` = \""  .$active_user. "\""; $row =
    mysql_fetch_array(mysql_query($sql)); //Link the two tables together; grab the most common thing
    that is the *SAME* $user_id = $row ; $sql2 = "SELECT * FROM content WHERE `cid` = \""  .$user_id.
    "\""; $row2 = mysql_fetch_array(mysql_query($sql2)); Then on the pages, I just do a where ever
    something is supp....
  6. Send XML Data To PHP Page
    (0)
    Hi, i'm trying to send my xml file "xmlDoc" to a php page so I can save it. Does anyone have the
    code for this?....
  7. Data Passing - Re An Assignment For School - Please Help :)
    (8)
    I'm working on a small assignment due tomorrow and am having some trouble. I have a functioning
    form that you input the data on one php page, and then it does a little math and displays the result
    on a new page. The assignment is to make this work on ONE page, and to add some error handling.
    I'm having trouble with the basics of passing the data that has been input on the form, back to
    itself. I am stuck with a few questions but I'll start with one. I have the data passing back
    to itself so that when I start the error checking, the fields won't have ....
  8. Need An Alternative To $http_post_data For PHP4
    (5)
    Hi, my client's host site currently hosts just 4.0. I tried using the
    file_get_contents("php://input") and $HTTML_post_data php file to save the XML file from Flash but
    when loaded, it returns nothing. I need hlep....
  9. Storing Data Into Xml With A Php Form
    Need Help! (2)
    Hi, I just learned how to read an xml file with PHP. The problem now is that I don't know how to
    write onto it. I would like to read my news content and be able to add more to it when another story
    comes up but I don't know how to write into the xml via PHP. All I know how to do is to edit the
    XML file itself manually. Can anyone help me?....
  10. Reading Data From Sessions
    (2)
    Hello everyone! Before I start let me just say that ive read many threads on here about php and
    sessions, several tutorials and the php manual but I still cant find a solution to my problem. My
    problem is that when i try to read data from a session it just comes up blank, and an if statement
    to see if the variable is null returns true. The code im using is for a login script which checks
    the input from a user and compares it with the data in a database to log the user in, this works
    fine, also reading the data from the session works perfectly on the page which the sess....
  11. [PHP + MySQL] Encrypting Data
    To protect the password of your DB, for example. (11)
    Hi! This is my 2nd code of PHP + MySQL. This code is VERY simple: it encript the data in the MySQL
    DB. Here we go! ------------------------------------------------------------------------ CODE
    $password = "abc"; $new_password = md5($password); echo $new_password; ?> The password "abc"
    was codfied using md5() This will be: 900150983cd24fb0d6963f7d28e17f72 CODE $normal_pass =
    "abc"; $encripted_pass = "900150983cd24fb0d6963f7d28e17f72"; if(md5($normal_pass) ==
    $encripted_pass)   echo "Login Sucessful!"; else   echo "Incorrect password."; ?> This c....
  12. Possible To Do?
    Reading a remote website's data .. (7)
    What I need to know is how to do the following? I'm completely confused, although I sort of
    have a general feel of what I need to do, I just have no clue how to do it. I need to be able to
    take some data from one page, displayed on their site as an HTML table, and then output certain
    pieces of it to my site. I will disclaim here and now that what I intend to do does not violate
    any laws, as I'm not stealing a persons content. I am using this to act as an online counter, to
    see how long someone is online. I also have emailed the company to ask if what I'm....
  13. Php/mysql Data Display
    (3)
    Okay .. got a bit of a question here, so I'll do some explaining. I was asked to do a site for
    an online roleplaying game, specifically, a "blackbook" site. I accepted and began my quest for
    knowledge of PHP. I've gotten quite "far" to the point that I can now take a user inputted
    search value and query the database with that value, and then display the values in a table. My
    MySQL table setup is as below: CODE |Name|Reports|Type1|Type2|Quote|Confirmed| When queried
    it displays the following: CODE Violator Name: # Reports: Offense(s): Quote: ....
  14. Help With Multi Tier Mysql Application Over Net
    receiving data connection from client (6)
    hi.. i want to make a connection from my desktop client into mysql database at web server.
    currently i think it can be provided by PHP. 1. i'm thinking like this: CLIENT -> PHP + MYSQL
    CLIENT {sent file} -> PHP {receive file, open connection to MYSQL, insert data from file} how it
    will be done ??? 2. the security do you know how to secure it ? thanks......
  15. Displaying Data From Mysql?
    (2)
    how can i display data from mysql with php, just that on one page i want to display only the first
    10 things and the next page the next 20 ...etc.. how can i do that? ....
  16. PHP Based Site Access Authentication - Help
    How to block parts of your web-site ?? (4)
    How can i program my web page using php that when the value of the login box is equal to some string
    then go to my success.html otherwise on my fail.html????help me guys!
    ------------------------------------ It would help the readers far better to understand what your
    problem is - if you state the nature of it in short in your topic title, instead of just "Php help".
    It'll also get you a lot more responses. Am changing your topic title to give you an example.
    All the best /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> m^e....
  17. Php, Sql Lite: Storing Session's Data?
    how so store session in SQLITE? (1)
    normally, in windows, session data is saved in the location as directed by the "session.save_path"
    directives. they only show how to store session data in file. is it possible to store it inside the
    SQLite? anyone?....

    1. Looking for parts, record, character, data






*SIMILAR VIDEOS*
Searching Video's for parts, record, character, data
advertisement




Getting Certain Parts Of A Record - The character data