如何在MySQL数据库中显示日期和价格以及如何在PHP中以各种格式显示日期和价格 [英] How to display date and price in a MySQL database and display it in various formats in PHP

查看:201
本文介绍了如何在MySQL数据库中显示日期和价格以及如何在PHP中以各种格式显示日期和价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php/mysql新手,所以我一直以通常在页面上显示日期的格式插入日期,例如:

I am a php/mysql novice so I have been inserting dates in whatever format I usually display dates on my pages, like:

Saturday, June 19, 2010

直接以纯文本格式输入数据库.但是我已经看到人们以其他方式输入它,然后使用一些php函数以各种格式显示相同的记录.这样做的方法是什么,以便我可以在数据库中保存一条记录,并以各种格式显示它:

directly into the database in plain text. But I have seen people enter it in some other way and then display that same record in various formats using some php function. What is the way to do this, so that I can have one record in the database and show it in various formats like:

6/19/2010
June 19, 2010
Saturday, June 19, 2010

等...

如何用钱做同样的事情?

How to do the same thing but with money?

推荐答案

如何处理,存储和更改php + mysql日期


对于日期,只需将其另存为DATETIME.它很容易阅读,也很容易在PHP中使用.您也可以选择使用纯TIMESTAMP,但我不建议这样做.

How to deal, store, change dates php + mysql


For dates just save them as a DATETIME. It's easy to read and easy to work with from within PHP. You could alternatively go with a pure TIMESTAMP but I would not recommend it.

DATETIME 的范围...
从"1000-01-01 00:00:00"到"9999-12-31 23:59:59".

DATETIME's range...
'1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

TIMESTAMP 的范围...
'1970-01-01 00:00:01'UTC到'2038-01-19 03:14:07'

TIMESTAMP's range...
'1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07'

不仅如此,而且您还具有许多内置的MYSQL函数来与DATETIME一起使用,例如INTERVAL,它将使您做的很棒.

Not only that but you also have a lot of build-in MYSQL functions to work with DATETIME's like INTERVAL which will allow you to do great things.

// easy pull all comments values from within a range
SELECT * FROM your_table
WHERE your_date_field = DATE_SUB('2001-01-01 12:12:12' + INTERVAL 15 DAYS;

如果您确实需要在PHP上使用时间戳,可以像这样在SQL上实时获取它们:

If you do need to work with timestamps on PHP you can pull them real time on SQL like this:

// converts a DATETIME field real time to timestamps
SELECT UNIX_TIMESTAMP(your_date_field)
FROM your_table (...)

当您拔出它们时,只要使用 strftime .下面是示例.

And when you pull them out just format them anyway you like using strftime. Example bellow.

// pull a date from MySQL and format them to a custom format
// %A -> full weekday | %B -> full month name | %d -> month day | %Y - year 1999
$sql = "SELECT title, name, UNIX_TIMESTAMP(date) as timestamp
        FROM your_table
        WHERE your_limits";
$result = mysql_fetch_assoc(mysql_query($sql));
echo strftime("%A, %B %d, %Y", $result['timestamp']);


如何在MySQL中存储货币


我会始终将它们存储在其上传的值中.仅当进行交易或更改时,才应将一种货币转换为另一种货币.否则,将保留以原始价格上传的值.

How to store currencies in MySQL


I would always store them in their uploaded value. Only when there is a transaction or a change should you convert from one currency to the other. Else keep the values as they were uploaded in their original price.

| 135.23 | USD |
| 200.35 | EUR |
|  34.00 | GBP |

显示货币时,您可以实时进行换算.只需用当前的交换日期保存一张单独的表格,然后进行实时数学运算即可.向用户显示您的转化率也将是一个加号.

When displaying the currencies you can make the conversion real time. Just keep a separate table with the current exchange date and do the math real time. It would also be a plus to show the user your conversion rates.

使用上面的帐户向上面的用户说要获取其欧元货币.

Say the user above with the above account wanted to get his currency in EUR.

135.23 USD = 109.39 EUR (1 USD = 0.80893059 EUR)
    30 GBP =  35.90 EUR (1 GBP = 1.19681281 EUR)
             200.35 EUR
-----------------------       
       total 345.64 EUR

这篇关于如何在MySQL数据库中显示日期和价格以及如何在PHP中以各种格式显示日期和价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆