Jump to content
Weiss

Working with dates

Recommended Posts

Hi. i have a table with a date row. i have 2 problems with it. first, it saves the date as mm-dd-yyyy and i actually need it to save it as dd-mm-yyyy.

the second problem is more of a question.. after a date has been saved, can i use it to calculate? can i calculate and output this date + day for example?

thanks in advance

Share this post


Link to post
Share on other sites

If you use any of the date types in mysql database it should save it as 'YYYY-MM-DD' only, if you save as text string you should be able to save as you want it. but! IF you wish to do calculations through MYSQL query's it expects the format to be as used by date types, OR if calculation of dates through PHP, it expects if year month and days used be be 'YYYY-MM-DD' formatting type with exception of hyphens where slashes could be used. Of course you could retrieve specific date ref from custom text string date, and put them in correct order for calculation, but that means more work, whereas you could reformat the database date on output to a page.

http://w3resource.com/mysql/date-and-time-functions/mysql-weekofyear-function.php

  • Like 1

Share this post


Link to post
Share on other sites

Thanks! so correct me if im wrong, by reading your comment, its best to keep the data in database as deafult  yyyy-mm-dd so i can have calculations. if my customer want to see it in dd-mm-yyyy i can retreive the data and manipulate it while echoing in the php code? is that what i should do?

Share this post


Link to post
Share on other sites

Yes! you can easily reformat the date format from database to whatever you want, with whatever separators you want on output using echo, see https://www.w3schools.com/php/showphp.asp?filename=demo_func_date_format

https://www.w3schools.com/php/php_date.asp

Its just a matter of swapping YY and DD on desired string format parameter

Edited by dsonesuk
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×