Alaa Abdelhaq Blog
20 Jan
You are having hard time with mysql datetime format? I wont blame you ![]()
I hope this short example will help you.
let say in your mysql database you are saving time on the datetime format
like this “2009-01-20 02:11:05″.
you need to pull it and add some days or time intervals and print it in a different format, thats easy do the following
1- In your query pulling the time, pull it this way:
UNIX_TIMESTAMP(time) AS time
this will covert it into unix timestamp, which will make it easier for you to manipulate it with php functions.
2- Add your time interval, let say we need to add 15 days.
$unix_timestamp_from_database = 1232410265; $time_from_db = $unix_timestamp_from_database; $new_time = $time_from_db + (15 * 86400);
86400seconds equals one day.
In unix timestamp we manipulate time by seconds.
so if you want to add an hour you add it like by (60 * 60)
which equals 3600 seconds.
3- Then you format it as you desire:
$new_time_format = date("d-M-Y", $new_time); echo $new_time_format;
hope it will help you
6 Responses for "How to manipulate mysql datetime using php"
It helped me
..
Thanks
what about DATEADD in mysql , its easier man
DATEADD may take your website down, and you know why
Some people also prefer to work it on the php level
yah maybe in huge result sets , but some times its easier to use dateadd , and some time its a must like in triggers and mysql stored functions
Thanks for your solution
You are most welcome Uma
Leave a reply