PHP, which looks beautiful

Source: Internet
Author: User

Recently worked on projects! A problem occurred due to my ignorance! I had to repeat the data again, and my work in the past four or five days was in vain. During this time, I posted and replied to the Forum and found a problem! Summary:

Looks beautiful

A: My questions

The data format shown in SQL Server 2000 is. I thought there was no time! Therefore, in the early stage, the PHP time processing function did not consider the time. During the final test, we found the timestamp written to MySQL. The calculated time is inconsistent with the record in SQL Server. because the data does not need to be displayed on the page, the work is in vain. After the data is found, I printed it on the page and output:

2004 14:23, January 1, March 2

Header ("Content-Type = text/html; charset = GBK ");

Problem found! The original format in the database is only for display convenience. In fact, the date is complete. In this case, QQ friends prompted me to display the display difference with the records in the database. Find a Formatting Function, you can do it. I am not familiar with functions when I first learned PHP. I checked the manual and found no functions similar to the function several times. I posted a post on the Forum:

Http://topic.csdn.net/u/20080610/13/27bee5f2-af07-4e3f-a75a-9f405fbd5549.html

The huigenius brother gave a piece of code:

Function formattimestyle ($ strdate) {$ search = array ('August 11', 'August 11', 'August 11', 'August 11', 'August 11', 'August 11 ', 'August 11', 'August 11', 'August 11', 'August 11'); $ replace = array ); $ date = str_replace ($ search, $ replace, $ strdate); $ darray = explode ('', $ date); $ date = array_pop ($ darray ); $ date = implode ('-', $ darray); return $ date ;}

I slightly changed it to meet yesterday's requirements. There is a birthday column in the user data table, and the data display is the same as above. In MySQL, we need three fields: year, month, and day, I will use the above function to format it:

$ Currstyle = formattimestyle ($ row [2])

To print:

print $row[2]."";print $currStyle."";

Remember to compare the output! Otherwise, you will not find the following errors:

2004 14:23, January 1, March 2

2004-3-2

Look pretty !, Compared with switch. Let's take a look at the description of the str_replace function.

Search for PHP str_replace on Google

Replace a substring with another string (Case Insensitive ).

Read the English description:

This function returns a string or an array with all occurrencesSearchInSubjectReplaced with the givenReplaceValue.

We will perform another test based on the rules of the first three and the last four:

$ Test = '2014, 2003 3:08 '; print $ test. ""; print formattimestyle ($ test )."";

Print Output:

2003 3:08, January 1, November 4

2003-10 1-4

Ah! Why not? Try again:

$ Test = '2014, 2003 3:08 '; print $ test. ""; print formattimestyle ($ test )."";

Print Output:

2003 3:08, January 1, October 4

2003-10-4

Normal! Are you confused? Try again:

$ Test = '2014, 2003 3:08 '; print $ test. ""; print formattimestyle ($ test )."";

Print Output:

2003 3:08, January 1, December 4

2003-10 2-4

Have you found any problems? The above format function is unqualified! Let's move a little less:

Function formattimestyle ($ strdate) {$ search = array ('August 11', 'August 11', 'August 11', 'August 11', 'August 11', 'August 11 ', 'August 11', 'August 11', 'August 11', 'August 11'); $ replace = array ); $ date = str_replace ($ search, $ replace, $ strdate); $ darray = explode ('', $ date); $ date = array_pop ($ darray ); $ date = implode ('-', $ darray); return $ date ;}

Test the test case with the original error:

$ Test = '2014, 2003 3:08 '; print $ test. ""; print formattimestyle ($ test )."";

Print Output:

2003 3:08, January 1, December 4

2003-12-4

Then the original normal test case:

$ Test = '2014, 2003 3:08 '; print $ test. ""; print formattimestyle ($ test )."";

Print Output:

2003 3:08, January 1, October 4

2003-10-4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.