hoangthi Posted October 21, 2013 Share Posted October 21, 2013 I get this from the table of database: 21-10-2013 14:30:00 Now, I want to + 5 days, 1 hour, It should be: 26-10-2013 15:30:00 How can I do that ? Please help me! Thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2013 Share Posted October 21, 2013 (edited) Using MySQL as the database? and you have a DATETIME column? You can use one of the mysql DATE functions within your query http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Alternatively use PHP's strtotime function and date to reformat the date $date = '21-10-2013 14:30:00'; $timestamp = strtotime('+5 days 1 hours', $date); echo date('l jS \of F Y h:i:s A', $timestamp); Edited October 21, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2013 Share Posted October 21, 2013 Store datetimes in "yyyy-mm-dd hh:ii:ss" format in DATETIME type columns. That way they are searchable, sortable, can be compared with other dates and, pertinent to this instance, you can use MySQL's date arithmetic functionality. In other words the correct format is useful, yours is not. If you want to do SQL arithmetic on your format you have the additional overhead of converting it from a string to the correct date format and back again SELECT DATE_FORMAT( STR_TO_DATE('21-10-2013 14:30:00', '%d-%m-%Y %H:%i:%s') + INTERVAL 5 DAY + INTERVAL 1 HOUR, '%d-%m-%Y %H:%i:%s') as newtime; +---------------------+ | newtime | +---------------------+ | 26-10-2013 15:30:00 | +---------------------+ Or you can do it in the PHP code $dt = DateTime::createFromFormat('d-m-Y H:i:s', '21-10-2013 14:30:00'); $dt->add(new DateInterval('P5DT1H')); echo $dt->format('d-m-Y H:i:s'); // --> 26-10-2013 15:30:00 Quote Link to comment Share on other sites More sharing options...
hoangthi Posted October 21, 2013 Author Share Posted October 21, 2013 Please help me this: $time = '03-10-2013 00:29:00'; echo strtotime($time); and the result is : 1380785340 = Thu, 03 Oct 2013 07:29:00 GMT, Not 03-10-2013 00:29:00 I don't understand why. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 21, 2013 Share Posted October 21, 2013 The following quote is from the manual (http://php.net/manual/en/function.strtotime.php): Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 21, 2013 Share Posted October 21, 2013 Sorry, I think I misunderstood the question. Is the question is that you're getting "07:29:00" for the time instead of "00:29:00"? If so what is the code you're using to format the date? The following <?php $time = '03-10-2013 00:29:00'; echo strtotime($time) . '<br>'; echo date('d-m-Y H:i:s', strtotime($time)); ?> ...gives the following output: 1380785340 03-10-2013 00:29:00 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2013 Share Posted October 21, 2013 Unfortunately, most people I know here in the UK would write the date as d/m/y Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2013 Share Posted October 21, 2013 Hoangthi Have you set your default timezone? http://php.net/manual/en/function.date-default-timezone-set.php Quote Link to comment Share on other sites More sharing options...
hoangthi Posted October 21, 2013 Author Share Posted October 21, 2013 (edited) Yes, Thanks, I did it Now I have 361764 seconds, How to convert it to aa days xx hours yy minutes zz second ? Edited October 21, 2013 by hoangthi Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2013 Share Posted October 21, 2013 try function secs2parts($secs) { $days = floor($secs/86400); $s = $secs % 86400; $hours = floor($s/3600); $s = $s % 3600; $mins = floor($s/60); $s = $s % 60; return array( 'days' => $days, 'hrs' => $hours, 'mins' => $mins, 'secs' => $s ); } foreach (secs2parts(361764) as $k=>$v) { if ($v) echo "$v$k "; } // --> 4days 4hrs 29mins 24secs Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.