简体   繁体   中英

PHP MySQL date functions

I am working with dates, in both PHP as well as MySQL. EVerytime I use to convert date in unix format. But this time I have taken field in DB as date. But issue is it is taking yyyy-mm-dd format. I want to store it in dd-mm-yyyy format. Is this possible if I set default setting of DB. or each time I have to explode the dd-mm-yyyy format in PHP and convert it in YYYY-MM-DD format. Its my first query.

Second query is I wish to fetch the records from today's date. I mean dates after today's date. Like today then tomorrow then so on.... Is it possible to use order by on date field.

Just use:

$date = date('d-m-Y', strtotime($dateFromDB));

That will convert from MySQL DateTime to the format you have specified.

It is possible to order by date fields, eg:

SELECT *
FROM table
WHERE date > [yourDate]
ORDER BY date [DESC | ASC]

Your second requirement contradicts with the first one.
If you store your date in dd-mm-yyyy format, you'll be unable to sort your dates.

So - yes, you have to "explode" the dd-mm-yyyy date in PHP or format it any other way. That's not a big deal though. Everyone does it.

If you have a field of type 'datetime' you can use the MySQL-Command: FROM_UNIXTIME(%d) for conversion. 'order by' should be no problem.

Store the date in default format that is yyyy-mm-dd

when you want to display in front end

use the following query to

select otherFields, date_format(dateField,'%d-%m-%Y') from tableName;

For ordering by date

SELECT * FROM tbl 
   ORDER BY date DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM