Jump to content

How can I + or - the time


hoangthi

Recommended Posts

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 by Ch0cu3r
Link to comment
Share on other sites

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 
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.