Dates in PHP and MySQL
I see a lot of people on forums and on my training courses asking about the best way (or any way) to manage dates stored in a MySQL database and used in PHP. Three options follow, but first the problem.
PHP uses unix timestamps for all its date functionality. It has methods to convert these timestamps into pretty much any text format you could want but internally it uses the timestamp format. A timestamp is simply an integer. Specifically, it’s the number of seconds that have elapsed since midnight on January 1st 1970 (greenwich mean time).
MySQL has three date types for use in columns. These are DATETIME, DATE, and TIMESTAMP. DATETIME columns store date and time as a string in the form YYYY-MM-DD HH:MM:SS (e.g. 2006-12-25 13:43:15). DATE columns use just the date part of this format – YYYY-MM-DD (e.g. 2006-12-25). TIMESTAMP columns, despite their name, are nothing like the unix timestamps used in PHP. A TIMESTAMP column is simply a DATETIME column that automatically updates to the current time every time the contents of that record are altered. (That’s a simplification but broadly true and the details are not important here). In particular, since version 4.1 of MySQL the TIMESTAMP format is exactly the same as the DATETIME format.
So the problem is how to work with these two very different date formats – the PHP timestamp integer and the MySQL DATETIME string. There are three common solutions…
One common solution is to store the dates in DATETIME fields and use PHPs date() and strtotime()functions to convert between PHP timestamps and MySQL DATETIMEs. The methods would be used as follows -
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );
Our second option is to let MySQL do the work. MySQL has functions we can use to convert the data at the point where we access the database. UNIX_TIMESTAMP will convert from DATETIME to PHP timestamp andFROM_UNIXTIME will convert from PHP timestamp to DATETIME. The methods are used within the SQL query. So we insert and update dates using queries like this -
$query = "UPDATE table SET
datetimefield = FROM_UNIXTIME($phpdate)
WHERE...";
$query = "SELECT UNIX_TIMESTAMP(datetimefield)
FROM table WHERE...";
Our last option is simply to use the PHP timestamp format everywhere. Since a PHP timestamp is a signed integer, use an integer field in MySQL to store the timestamp in. This way there’s no conversion and we can just move PHP timestamps into and out of the database without any issues at all.
Be aware, however, that by using an integer field to store your dates you lose a lot of functionality within MySQL because MySQL doesn’t know that your dates are dates. You can still sort records on your date fields since php timestamps increase regularly over time, but if you want to use any of MySQL’s date and time functions on the data then you’ll need to use FROM_UNIXTIME to get a MySQL DATETIME for the function to work on.
However, if you’re just using the database to store the date information and any manipulation of it will take place in PHP then there’s no problems. www.2cto.com
So finally we come to the choice of which to use. For me, if you don’t need to manipulate the dates within MySQL then there’s no contest and the last option is the best. It’s simple to use and is the most efficient in terms of storage space in the data table and speed of execution when reading and writing the data.
However, some queries will be more complicated because your date is not in a date field (e.g. select all users who’s birthday is today) and you may lose out in the long run. If this is the case it may be better to use either option 1 or 2. Which of these you use depends on whether you’d rather place the work on MySQL or PHP. I tend to use option 2 but there’s no right or wrong answer – take your pick.
So to summarise, for those who’ve skipped straight to the last paragraph, most of the time I use option 3 but occasionally I use option 2 because I need MySQL to know the field contains a date.
作者:chenjie3392593

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas











MySQL dan phpmyadmin adalah alat pengurusan pangkalan data yang kuat. 1) MySQL digunakan untuk membuat pangkalan data dan jadual, dan untuk melaksanakan pertanyaan DML dan SQL. 2) Phpmyadmin menyediakan antara muka intuitif untuk pengurusan pangkalan data, pengurusan struktur meja, operasi data dan pengurusan kebenaran pengguna.

Di MySQL, fungsi kunci asing adalah untuk mewujudkan hubungan antara jadual dan memastikan konsistensi dan integriti data. Kekunci asing mengekalkan keberkesanan data melalui pemeriksaan integriti rujukan dan operasi cascading. Perhatikan pengoptimuman prestasi dan elakkan kesilapan biasa apabila menggunakannya.

IIS dan PHP serasi dan dilaksanakan melalui FastCGI. 1.IIS meneruskan permintaan fail .php ke modul FastCGI melalui fail konfigurasi. 2. Modul FastCGI memulakan proses PHP untuk memproses permintaan untuk meningkatkan prestasi dan kestabilan. 3. Dalam aplikasi sebenar, anda perlu memberi perhatian kepada butiran konfigurasi, debugging ralat dan pengoptimuman prestasi.

Perbezaan utama antara MySQL dan MariaDB adalah prestasi, fungsi dan lesen: 1. MySQL dibangunkan oleh Oracle, dan Mariadb adalah garpu. 2. MariaDB boleh melakukan lebih baik dalam persekitaran beban tinggi. 3.MariADB menyediakan lebih banyak enjin dan fungsi penyimpanan. 4.MYSQL mengamalkan lesen dua, dan MariaDB adalah sumber terbuka sepenuhnya. Infrastruktur yang sedia ada, keperluan prestasi, keperluan fungsional dan kos lesen perlu diambil kira apabila memilih.

Pelbagai panggilan ke session_start () akan menghasilkan mesej amaran dan kemungkinan penggantian data. 1) PHP akan mengeluarkan amaran, menyebabkan sesi telah dimulakan. 2) Ia boleh menyebabkan penggantian data sesi yang tidak dijangka. 3) Gunakan session_status () untuk memeriksa status sesi untuk mengelakkan panggilan berulang.

SQL adalah bahasa standard untuk menguruskan pangkalan data relasi, manakala MySQL adalah sistem pengurusan pangkalan data yang menggunakan SQL. SQL mentakrifkan cara untuk berinteraksi dengan pangkalan data, termasuk operasi CRUD, sementara MySQL melaksanakan standard SQL dan menyediakan ciri -ciri tambahan seperti prosedur dan pencetus yang disimpan.

AI boleh membantu mengoptimumkan penggunaan komposer. Kaedah khusus termasuk: 1. Pengoptimuman Pengurusan Ketergantungan: AI menganalisis kebergantungan, mengesyorkan kombinasi versi terbaik, dan mengurangkan konflik. 2. Generasi Kod Automatik: AI menjana fail komposer.json yang sesuai dengan amalan terbaik. 3. Meningkatkan Kualiti Kod: AI mengesan masalah yang berpotensi, memberikan cadangan pengoptimuman, dan meningkatkan kualiti kod. Kaedah ini dilaksanakan melalui pembelajaran mesin dan teknologi pemprosesan bahasa semulajadi untuk membantu pemaju meningkatkan kecekapan dan kualiti kod.

MySQL dan phpmyadmin boleh diuruskan dengan berkesan melalui langkah -langkah berikut: 1. Buat dan hapus pangkalan data: hanya klik phpmyadmin untuk diselesaikan. 2. Mengurus Jadual: Anda boleh membuat jadual, mengubahsuai struktur, dan menambah indeks. 3. Operasi Data: Menyokong memasukkan, mengemas kini, memadam data dan melaksanakan pertanyaan SQL. 4. Data Import dan Eksport: Menyokong SQL, CSV, XML dan format lain. 5. Pengoptimuman dan Pemantauan: Gunakan arahan yang boleh dioptimumkan untuk mengoptimumkan jadual dan gunakan penganalisis pertanyaan dan alat pemantauan untuk menyelesaikan masalah prestasi.
