简体   繁体   中英

PHP Get date today and yesterday

在此处输入图片说明

may data > 3 If there is a change/update.

How to set logic to place date (yesterday/today), I sent 2 parameter when check-in (present)

Example i'm present at 2020-09-04 01:24. There is 1 data shift that must be different on the date

i want to set shift with date like this.

time in : 2020-09-03 22:00

time out : 2020-09-04 02:00

This is my code

<?php
$date = '2020-09-03';
$time = '01:50';

$arr = [];
$data_1 = array(
    'time_in' =>'04:00',
    'time_out'=>'10:00');
$data_2 = array(
    'time_in' =>'12:00',
    'time_out'=>'21:00');
$data_3 = array(
    'time_in' =>'22:00',
    'time_out'=>'02:00');
$arr[] = $data_1;
$arr[] = $data_2;
$arr[] = $data_3;

$new_data = [];
foreach($arr as $v){
    $time_in  = $v['time_in'];
    $time_out = $v['time_out'];
    
    $push_with_date = array(
    'time_in' =>$time_in,
    'time_out'=>$time_out);
        
    $new_data[] = $push_with_date; // i want push time with date
}

echo "<pre>";
print_r($new_data);
echo "</pre>";
?>

Help me thank's

I made a code for you. please check the logic.

function changeHour($time) {
  $times = explode(":", $time);
  return (count($times) > 1) ? intval($times[0]) * 60 + intval($times[1]) : intval($times[0]) * 60;
}

function is_yesterday($time1, $time2) {
  if (changeHour($time1) <= changeHour($time2)) {
    return false;
  }

  return true;
}

$date = '2020-09-03';
$time = '01:50';

$ydate = date("Y-m-d",strtotime($date) - 86400);



$arr = [];
$data_1 = array(
    'time_in' =>'04:00',
    'time_out'=>'10:00');
$data_2 = array(
    'time_in' =>'12:00',
    'time_out'=>'21:00');
$data_3 = array(
    'time_in' =>'22:00',
    'time_out'=>'02:00');
$arr[] = $data_1;
$arr[] = $data_2;
$arr[] = $data_3;

$new_data = [];
foreach($arr as $v){
    $time_in  = $v['time_in'];
    $time_out = $v['time_out'];
    
    if (is_yesterday($time, $time_out)) {

      $time_in = "$ydate $time_in";
      $time_out = "$ydate $time_out";
      
    } else {
      
      if (is_yesterday($time_in, $time_out)) {
        $time_in = "$ydate $time_in";
      } else {
        $time_in = "$date $time_in";
      }
      
      $time_out = "$date $time_out";      
    }
    $push_with_date = array(
    'time_in' =>$time_in,
    'time_out'=>$time_out);
        
    $new_data[] = $push_with_date; // i want push time with date
}

echo "<pre>";
print_r($new_data);
echo "</pre>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM