将字符串转换为日期(欧洲到美国) [英] Convert String to date (european to american)

查看:349
本文介绍了将字符串转换为日期(欧洲到美国)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个字符串:dd / mm-yyyy,其中dd是两位数的月份的日期,mm是用两位数字表示的月份,yyyy是四位数的年份。

我需要将其存储在我的数据库中。我试过了

$ $ p $ $ code $ dato = date('d / m-Y',strtotime($ _ POST ['dag'])

但是这显然不起作用,在我的数据库中日期显示为yyyy-mm-dd。正确地将字符串转换为正确的格式? strtotime 不接受你的时间字符串格式,所以它返回false。你可以使用 DateTime :: createFromFormat date_create_from_format ,手动此处

$ p $ DateTime :: createFromFormat('d / m-Y',$ _POST ['dag']);

检查现场演示

 <?php 

var_dump(DateTime :: createFromFormat('d / m-Y','11 / 01-2017'));

输入:

  object (DateTime)#1(3){
[date] =>
string(26)2017-01-11 14:34:53.000000
[timezone_type] =>
int(3)
[timezone] =>
string(3)UTC
}


I have a String on the form: "dd/mm-yyyy", where dd is day of the month with two digits, mm is the month represented with two digits, and yyyy is the year with four digits.

I need to store this in my database. Ive tried

$dato = date('d/m-Y' ,strtotime($_POST['dag'] )

But that clearly doesnt work. In my database the date displays as yyyy-mm-dd. How do I properly convert the String to the correct format?

解决方案

strtotime not accept your time string format, so it return false. You can use DateTime::createFromFormat or date_create_from_format, manual here.

DateTime::createFromFormat('d/m-Y', $_POST['dag']);

check the live demo

<?php

var_dump(DateTime::createFromFormat('d/m-Y', '11/01-2017'));

putput:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2017-01-11 14:34:53.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}

这篇关于将字符串转换为日期(欧洲到美国)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆