如何在此字符串中检查日期是否连续?
$dates = "2017-01-08, 2017-01-09, 2017-01-10";
对于此示例,3个日期是连续的。
$dates = "2017-01-08, 2017-01-12, 2017-01-13";
对于此示例,3个日期不是连续的。
对此有何帮助?
感谢。
答案 0 :(得分:1)
使用DateTime::diff
功能,您可以获得天数差异。然后遍历日期,如果差异不是一天,则不是连续的。
$dates = "2017-01-08, 2017-01-09, 2017-01-10";
var_dump(check_continuous_dates($dates)); // true
$dates = "2017-01-08, 2017-01-12, 2017-01-13";
var_dump(check_continuous_dates($dates)); // false
function check_continuous_dates($date) {
$date = explode(", ", $date); // Convert it to an array we can loop
$previous = new DateTime($date[0]); // Set the "previous" value
unset($date[0]); // Unset the value we just set to $previous, so we don't loop it twice
foreach ($date as $v) { // Loop the array
$current = new DateTime($v);
$diff = $current->diff($previous);
// If the difference is exactly 1 day, it's continuous
if ($diff->days == 1) {
$previous = new DateTime($v);
} else {
return false;
}
}
return true;
}
答案 1 :(得分:0)
ArrayList<String> arrTblNames = new ArrayList<>();
List<List<String>> data = new ArrayList<>();
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
if (c.moveToFirst()) {
while ( !c.isAfterLast() ) {
arrTblNames.add( c.getString( c.getColumnIndex("name")) );
c.moveToNext();
}
}
for(int i=0; i<=arrTblNames.size()-1;i++){
Cursor dbCursor = db.query(arrTblNames.get(i), null, null, null, null, null, null);
String[] columnNames = dbCursor.getColumnNames();
Cursor c2 = db.query(arrTblNames.get(i),columnNames,null,null,null,null,null);
if (c2.moveToFirst()) {
while ( !c2.isAfterLast() ) {
data.add(new ArrayList<String>());
for(int j=0; j<=columnNames.length-1;j++){
data.get(i).add( c2.getString( c2.getColumnIndex(columnNames[j])) );
}
c2.moveToNext();
}
}
}