我试图在两个日期之间进行比较,但不幸的是,通过strtotime
将其转换为UNIX格式,这不起作用。我试图将日期与另一个日期进行比较。
但是这种格式有效:
if(strtotime("22-04-17") < strtotime("25-05-17")){
echo 'Date One is smaller than date two';
}
但很多时候它都失败了。我在网上看过很多例子,但我找不到任何好的东西!
if(strtotime("22-04-17") < strtotime("04-05-17")){ //passing still the
// bigger on but not working
echo 'Date One is smaller than date two';
}
答案 0 :(得分:3)
来自manual(特别注意我输入的部分粗体):
“m / d / y或dmy格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠(/),则假定为美国m / d / y;而如果分隔符是短划线( - )或点(。),然后假定为欧洲dmy格式。但是,如果年份以两位数格式给出,而分隔符是短划线( - ,日期string被解析为ymd。“
所以这就是你正在做的事情,PHP正在解释你的字符串的日期在评论中:
// Is 17 April 2022 earlier than 17 May 2025? Yes.
if(strtotime("22-04-17") < strtotime("25-05-17")){
echo 'Date One is smaller than date two';
}
// Is 17 April 2022 earlier than 17 May 2004? No.
if(strtotime("22-04-17") < strtotime("04-05-17")){ //passing still the
// bigger on but not working
echo 'Date One is smaller than date two';
}
我希望这会让您解决问题。
正如手册中所述,如果您想避免含糊不清,请使用DateTime::createFromFormat
/ date_create_from_format
:
$date = date_create_from_format('d-m-y', '04-05-17'); // 4 May 2017
答案 1 :(得分:2)
试试这个
$date1 = date('d-m-y',strtotime("22-04-17"));
$date2 = date('d-m-y',strtotime("04-05-17"));;
if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the
echo 'Date One is smaller than date two';
}
您的年份格式17
导致strtotime
功能
答案 2 :(得分:1)
您的比较无效,因为strtotime("22-04-17")
实际上会导致此日期的时间戳:17th April 2022
;
执行以下操作,您将看到我的意思。以下代码将输出&#39; 2022-May-17`
$date = "22-05-17";
echo date ("Y-M-d ",strtotime($date))."<br>";
答案 3 :(得分:0)
我知道这不是你想要的,但是你试过这样做来显示日期是否更大/更小?
// Dates
$Date1 = strtotime("22-04-17 GMT");
$Date2 = strtotime("04-05-17 GMT");
// Diff between dates
$Diff = (int)floor(($Date1 - $Date2) / (60*60*24));
if ($Diff < 0) {
echo "The Diff is negative";
}
然后另一种方式就像这个答案:LINK
$date1 = strtotime("22-04-17 GMT");
$date2 = strtotime("04-05-17 GMT");
if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the
echo 'Date One is smaller than date two';
}
答案 4 :(得分:0)
我写了一个函数,它接受你的日期字符串并正确返回timestmp。请注意,我遵循PHP的2位数年份惯例,i.e. 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999
/* functon takes dateString of format dd-dd-yy and return proper timestamp */
function getTimestamp($dateString)
{
$res = preg_match('/^([0-9]{2})\-([0-9]{2})-([0-9]{2})/', $dateString, $matches);
if(!$res)
return 0;
//00-69 are mapped to 2000-2069 and 70-99 to 1970-1999
if($matches[3]>=70 && $matches[3]<=99 )
$year = "19".$matches[3];
else
$year = "20".$matches[3];
$formatted_dat_string = $year."-".$matches[2]."-".$matches[1];
return strtotime($formatted_dat_string);
}
getTimestamp("22-04-99");
因此,您现在可以使用此函数代替strtotime
进行比较。
答案 5 :(得分:0)
最后,我得到了解决方案。 dateTime
对处理这种情况没有任何好处。您应该转而使用//First you need to pass the original format then you will need to pass new
//Format to get this working properly. Hope this will help you guy's
$myDateTimestart = DateTime::createFromFormat('d-m-Y', $dateString);
$startDate = $myDateTimestart->format('m-d-Y');
//with Simply that you can format your date properly
对象。
//* Linear Layout *
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setTag("linearLayout" + Integer.toString(id));
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundResource(R.drawable.background_border_1);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 70);
layoutParams.setMargins(20, 10, 20, 10);
linearLayout.setWeightSum(10);
linearLayout.setLayoutParams(layoutParams);
Toast.makeText(getBaseContext(), Integer.toString((int)linearLayout.getWeightSum())+ ", " + Integer.toString( linearLayout.getHeight()) + ", " + Integer.toString( linearLayout.getWidth()), Toast.LENGTH_LONG).show();
//* Edit Text *
EditText editText = new EditText(this);
editText.setTag("editText" + Integer.toString(id));
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = 50;
layoutParams.setMargins(10, 0, 10, 0);
layoutParams.weight = 2;
editText.setEms(10);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setLayoutParams(layoutParams);
editText.setGravity(Gravity.CENTER);
//* Text View *
TextView textView = new TextView(this);
textView.setTag("textView" + Integer.toString(id));
layoutParams.height = 50;
layoutParams.setMargins(10, 0, 10, 0);
layoutParams.weight = 4;
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER);
textView.setText("Credit Hours");
//* Spinner *
Spinner spinner = new Spinner(this);
spinner.setTag("spinner" + Integer.toString(id));
layoutParams.width = 50;
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
spinner.setGravity(Gravity.FILL_VERTICAL | Gravity.CENTER | Gravity.CENTER_VERTICAL);
layoutParams.setMargins(10, 0, 10, 0);
layoutParams.weight = 4;
spinner.setLayoutParams(layoutParams);
linearLayout.addView(editText);
linearLayout.addView(textView);
linearLayout.addView(spinner);
llSubjectsContainer = (LinearLayout) findViewById(R.id.llSubjectsContainer);
llSubjectsContainer.addView(linearLayout);
我只是把这件事搞砸了,这真的很糟糕