PHP将日期时间舍入到最接近的5个区间

时间:2017-11-15 06:42:40

标签: php date datetime php-5.6

以下是要求,我以格式

给出了日期时间
  

11/14/2017 22:36

它应该是最近的5分钟间隔,如下所示:

public class buscador extends AppCompatActivity {

ListView lv;
private DatabaseReference myRef,ref,ref2;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListner;
private String id;
private FirebaseDatabase mFirebaseDatabase;
private static final String TAG = "buscador";
int num=1;
private ArrayList<String> array2;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscador);

    lv=(ListView)findViewById(R.id.cat1);
    EditText search = (EditText)findViewById(R.id.searchfind);
    mAuth=FirebaseAuth.getInstance();
    FirebaseUser user=mAuth.getCurrentUser();
    id=ref.getKey();

    setupFirebaseAuth();


    ref=FirebaseDatabase.getInstance().getReference("Products");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
       public void onDataChange(DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
public void showData(DataSnapshot dataSnapshot){
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        Producto info = new Producto();
        info.setName(ds.child(id).getValue(Producto.class).getName());

        Log.d(TAG, "showData:name:" + info.getName());
        ArrayList<String> array=new ArrayList<>();
        array.add(info.getName());
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
        lv.setAdapter(adapter);}
}

1 个答案:

答案 0 :(得分:0)

感谢此帖https://stackoverflow.com/a/26103014/3481654

该公式为

$time = round(time() / 300) * 300;

完整的工作代码

function nearest5Mins($time) {
  $time = (round(strtotime($time) / 300)) * 300;
  return date('Y-M-d H:i', $time);
}
echo nearest5Mins('11/14/2017 22:48');
echo "<br>";
echo nearest5Mins('11/14/2017 11:23');

DEMO