Php 自定义字段的日期外观错误
我只是尝试在WooCommerce基于和线程的结账过程中添加一个“交货日期” 这是在订单元上打印交货日期并在感谢页面和管理订单页面上查看的相关代码:Php 自定义字段的日期外观错误,php,date,woocommerce,action,Php,Date,Woocommerce,Action,我只是尝试在WooCommerce基于和线程的结账过程中添加一个“交货日期” 这是在订单元上打印交货日期并在感谢页面和管理订单页面上查看的相关代码: //Shipping (Delivery) Date // Add custom checkout datepicker field add_action( 'woocommerce_before_order_notes', 'checkout_display_datepicker_custom_field' ); function chec
//Shipping (Delivery) Date
// Add custom checkout datepicker field
add_action( 'woocommerce_before_order_notes', 'checkout_display_datepicker_custom_field' );
function checkout_display_datepicker_custom_field( $checkout ) {
$field_id = 'my_datepicker';
echo '<div id="datepicker-wrapper">';
$today = strtotime('today');
$tomorrow = strtotime('tomorrow');
$dayAfterTomorrow = strtotime('+2 days');
woocommerce_form_field( $field_id, array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Choose a date'),
'placeholder' => __('Select delivery date'),
'required' => true, // Or false
'options' => array(
'' => 'Select',
date( 'yyyy-mm-dd', $today ) => date( get_option('date_format'), $today ),
date( 'yyyy-mm-dd', $tomorrow ) => date( get_option('date_format'), $tomorrow ),
date( 'yyyy-mm-dd', $dayAfterTomorrow ) => date( get_option('date_format'), $dayAfterTomorrow ),
)));
echo '<br></div>';
}
// Save field
add_action( 'woocommerce_checkout_create_order', 'save_datepicker_custom_field_value', 10, 2 );
function save_datepicker_custom_field_value( $order, $data ){
$field_id = 'my_datepicker';
$meta_key = '_'.$field_id;
if ( isset($_POST[$field_id]) && ! empty($_POST[$field_id]) ) {
$date = esc_attr($_POST[$field_id]);
$order->update_meta_data( $meta_key, $date ); // Save date as order meta data
$note = sprintf(__("Chosen date for Thank you page: %s.", "woocommerce"), $date );
$note = isset($data['order_comments']) && ! empty($data['order_comments']) ? $data['order_comments'] . '. ' . $note : $note;
// Save date on customer order note
$order->set_customer_note( $note );
}
}
//装运(交付)日期
//添加自定义签出日期选择器字段
添加操作(“订单注释前的woocommerce”、“签出显示”、“日期选择器”、“自定义字段”);
函数签出\显示\日期选择器\自定义\字段($checkout){
$field_id='my_datepicker';
回声';
$today=标准时间(“今天”);
$明天=标准时间(“明天”);
$dayAfterMorrow=strtotime(“+2天”);
woocommerce\u表单\u字段($field\u id,数组)(
'类型'=>'选择',
'class'=>array('my-field-class表单行宽'),
“label'=>”(选择日期),
“占位符”=>(选择交货日期),
“必需”=>true、//或false
“选项”=>数组(
''=>'选择',
日期('yyyy-mm-dd',$today)=>date(获取选项('date\u格式'),$today),
日期('yyyy-mm-dd',$moother)=>date(获取选项('date\u格式'),$moother),
日期('yyyy-mm-dd',$dayAfterMorrow)=>date(获取选项('date\u格式'),$dayAfterMorrow),
)));
回声“
”;
}
//保存字段
添加操作('woocommerce\u checkout\u create\u order'、'save\u datepicker\u custom\u field\u value',10,2);
函数save_datepicker_custom_field_value($order,$data){
$field_id='my_datepicker';
$meta_key=''.'$field_id;
if(设置($\u POST[$field\u id])&&!空($\u POST[$field\u id])){
$date=esc_attr($_POST[$field_id]);
$order->update_meta_data($meta_key,$date);//将日期另存为订单元数据
$note=sprintf(uuuuuuuuuuuuuuuuuuuuuu(“感谢页面选择的日期:%s.,“woocommerce”),$date);
$note=isset($data['order\u comments'])和&!empty($data['order\u comments'])?$data['order\u comments'])。$note:$note;
//在客户订单注释上保存日期
$order->set\u customer\u note($note);
}
}
它打印所选日期,但格式错误,如:202020-1212-2424
如何修改此错误?您在
date()
函数上使用了错误的格式。
看一下文档
在您的情况下,yyyy-mm-dd
:
一年的两位数表示法y
一个月的数字表示,前导零m
月份的第几天,两位数字加前导零d
202020-1212-2424
你的意思可能是2020-12-24
,应该是Y-m-d
:
年的完整数字表示,4位数字Y