-4

Is there anyone knows this.. I am new in php..

the date format is => D/M/Y H:M

$date = array (
"6/22/2015 7:14",
"6/22/2015 7:20",
"6/22/2015 7:20",
"6/22/2015 7:21",
"6/22/2015 7:29",
"6/22/2015 7:30",
"6/22/2015 7:30",
"6/22/2015 7:35",
"6/22/2015 7:38",
"6/23/2015 7:40",
"6/23/2015 7:44",
"6/23/2015 7:44",
"6/23/2015 7:46",
"6/23/2015 7:47",
"6/23/2015 7:48"
);

now I want to add(+) 1 whose date is 6/22/2015 and sub(-) whose date is 6/23/2015..........

desired output:

6/22/2015 8:14
6/22/2015 8:20
6/22/2015 8:20
6/22/2015 8:21
6/22/2015 8:29
6/22/2015 8:30
6/22/2015 8:30
6/22/2015 8:35
6/22/2015 8:38
6/23/2015 6:40
6/23/2015 6:44
6/23/2015 6:44
6/23/2015 6:46
6/23/2015 6:47
6/23/2015 6:48

i dont have code yet coZ i dont know where to start.. Please consider me...

Bee
  • 309
  • 3
  • 14
  • Stack Overflow is not a free code writing service. Just try something until you get stuck. – Rizier123 Sep 01 '15 at 14:25
  • [PHP Docs](http://php.net/manual/en/ref.datetime.php) or [a search](https://duckduckgo.com/?q=php+date+time) would be the place to start. – Reed Sep 01 '15 at 14:26
  • 1
    possible duplicate of [Adding days to $Date in PHP](http://stackoverflow.com/questions/3727615/adding-days-to-date-in-php) – Thiago Augustus Oliveira Sep 01 '15 at 14:27

3 Answers3

1

You can you the DateTime class:

<?php
$date = new DateTime('10/12/2001 7:14');

/*The following adds one day, 
 use new DateInterval('PT1H') for 1 hour */
$date->add(new DateInterval('P1D'));     

echo $date->format('d/m/Y g:i') . "\n";

for subtraction :

$date->sub(new DateInterval('P1D'));

DateTime::add()

donquixote
  • 411
  • 2
  • 16
0

You can try this:

$date = array (
"6/22/2015 7:14",
"6/22/2015 7:20",
"6/22/2015 7:20",
"6/22/2015 7:21",
"6/22/2015 7:29",
"6/22/2015 7:30",
"6/22/2015 7:30",
"6/22/2015 7:35",
"6/22/2015 7:38",
"6/23/2015 7:40",
"6/23/2015 7:44",
"6/23/2015 7:44",
"6/23/2015 7:46",
"6/23/2015 7:47",
"6/23/2015 7:48"
);

foreach($date as $key => $value) {
   $date[$key] = date('m/d/Y H:i', strtotime($value. ' +1 hours'));
}

print_r($date);
0

This php code also filters the correct dates for +1 hour and -1 hour

<?php

$date = array (
"6/22/2015 7:14",
"6/22/2015 7:20",
"6/22/2015 7:20",
"6/22/2015 7:21",
"6/22/2015 7:29",
"6/22/2015 7:30",
"6/22/2015 7:30",
"6/22/2015 7:35",
"6/22/2015 7:38",
"6/23/2015 7:40",
"6/23/2015 7:44",
"6/23/2015 7:44",
"6/23/2015 7:46",
"6/23/2015 7:47",
"6/23/2015 7:48"
);

foreach($date as $key => $value) {

    if (date('m/d/Y', strtotime($value)) == '06/22/2015')
      $time = $value . ' +1 hours';  
    elseif (date('m/d/Y', strtotime($value)) == '06/23/2015')
      $time = $value . ' -1 hours';  
    else  
      $time = $value;

    $date[$key] = date('m/d/Y H:i', strtotime($time));  
}

echo '<pre>';
var_dump($date);
echo '</pre>';

?>

Output

array(15) {
  [0]=>
  string(16) "06/22/2015 08:14"
  [1]=>
  string(16) "06/22/2015 08:20"
  [2]=>
  string(16) "06/22/2015 08:20"
  [3]=>
  string(16) "06/22/2015 08:21"
  [4]=>
  string(16) "06/22/2015 08:29"
  [5]=>
  string(16) "06/22/2015 08:30"
  [6]=>
  string(16) "06/22/2015 08:30"
  [7]=>
  string(16) "06/22/2015 08:35"
  [8]=>
  string(16) "06/22/2015 08:38"
  [9]=>
  string(16) "06/23/2015 06:40"
  [10]=>
  string(16) "06/23/2015 06:44"
  [11]=>
  string(16) "06/23/2015 06:44"
  [12]=>
  string(16) "06/23/2015 06:46"
  [13]=>
  string(16) "06/23/2015 06:47"
  [14]=>
  string(16) "06/23/2015 06:48"
}
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34