Jump to content

change date after midnight


canabatz
Go to solution Solved by Barand,

Recommended Posts

hi all

 

i have an array that hold times as follow(i define the date):

2016-02-24 16:55
2016-02-24 17:55
2016-02-24 19:55
2016-02-24 23:55
2016-02-24 01:34 -<----here is my problem , i want the date to change if it's after midnight.


it shuold be like that automaticly:
2016-02-25 01:34

 

somthing like that:

$day="2016-02-26";

if(strtotime(01:34) > strtotime("23:59"))
	{
	$day =  date('Y-m-d',strtotime($day."+1 days"));
	}
	else
	{
		//KEEP CURRENT DATE
	}

the time im geting from a web site with file_get_contents and there the date do not change if after midnight so i got to do it my self :)

 

any help ?

 

thanks

Edited by canabatz
Link to comment
Share on other sites

  • Solution

here's one way

$arr = [   '2016-02-24 16:55',
           '2016-02-24 17:55',
           '2016-02-24 19:55',
           '2016-02-24 23:55',
           '2016-02-24 00:35',
           '2016-02-24 01:34' 
       ];

for ($i=1, $j=0, $k=count($arr); $i<$k; $i++, $j++) {
    $dj = new DateTime($arr[$j]);
    $di = new DateTime($arr[$i]);
    if ($di < $dj) {
        $arr[$i] = $di->modify('+1 days')->format('Y-m-d H:i');
    }
}
echo '<pre>',print_r($arr, true),'</pre>';
 


/* RESULT *************
    Array
    (
        [0] => 2016-02-24 16:55
        [1] => 2016-02-24 17:55
        [2] => 2016-02-24 19:55
        [3] => 2016-02-24 23:55
        [4] => 2016-02-25 00:35
        [5] => 2016-02-25 01:34
    )
****************************/
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.