Skip to main content

Posts

Showing posts from March, 2024

Covert all date data format from VARCHAR to DATE in any MySQL table

 Converting varchar data to date format in MySQL involves several steps. Here's a method to achieve this: Assuming your varchar date column is named date_column and your table is named your_table, you can follow these steps: Add a New Date Column: First, add a new date column to your table. ALTER TABLE your_table ADD new_date_column DATE; Update New Date Column: Update the newly added date column using the STR_TO_DATE function to convert the varchar dates to date format. UPDATE your_table SET new_date_column = STR_TO_DATE(date_column, 'your_date_format'); Replace 'your_date_format' with the format of the varchar dates in your column. For example, if your dates are in the format 'YYYY-MM-DD', use '%Y-%m-%d'.  Drop Old Date Column: If you're confident that the new date column contains the correct data, you can drop the old varchar date column. ALTER TABLE your_table DROP COLUMN date_column; Rename New Date Column: Finally, rename the new date colum

How to reverse date in PHP

 To reverse the date from the format "yyyy-mm-dd" to "mm-dd-yyyy" in PHP, you can use the following script: <?php $date = "2023-07-04" ; // Reversing the date format $reversedDate = date ( "m-d-Y" , strtotime ( $date )); echo "Original date: " . $date . "<br>" ; echo "Reversed date: " . $reversedDate ; ?> In this script, the variable $date holds the original date in the "yyyy-mm-dd" format. The date() function is then used to convert and format the date. By passing the $date variable to strtotime(), it is converted to a Unix timestamp, which can be easily manipulated. The date() function then reformats the timestamp to the desired "mm-dd-yyyy" format. Finally, the original and reversed dates are printed on the screen.