【Django中datetime的处理方法(strftime/strptime)】教程文章相关的互联网学习教程文章

PHP datetime字段SELECT今天?【代码】

我今天无法获取我的select语句来过滤datetime列:我只需要rowcount(多少)$u_today = date('Y-m-d'); $query = mysql_query("SELECT * FROM wds_sec1tasks WHERE due_date = '$u_today'"); $u_rows = mysql_num_rows($query); echo $u_rows;我需要抓住日期时间的左边10个字符进行比较吗? SUBSTR(DUE_DATE,0,10)? 谢谢,JM解决方法:您必须首先在DATE中转换due_date,方法是这样做:SELECT * FROM wds_sec1tasks WHERE DATE(due_date)...

PHP错误的DateTime :: diff()返回错误的DateInterval【代码】

我有两个日期时间差异的问题.以下是显示DateInterval对象的命令行:php -r "\$a = new Datetime('first day of 4 months ago midnight'); \$b = new Datetime('first day of 1 month ago midnight'); var_dump(\$a->diff(\$b));"这里是DateInterval输出:class DateInterval#3 (15) {public $y => int(0)public $m => int(3)public $d => int(3)public $h => int(0)public $i => int(0)public $s => ...

处理PHP DateTime中格式正确但无效的日期【代码】

我正在尝试使用PHP的DateTime来验证日期,但DateTime将接受一些格式正确但无效的日期,如2015-02-30,它将变成2015年3月2日,而不会抛出异常.使用DateTime或其他方法处理此问题的任何建议? 编辑:谢谢大家的帮助!我通过捕获异常来处理错误,但我没有意识到异常只是抛出错误而不是警告,而这种输入只会发出警告.解决方法:使用DateTime :: getLastErrors()检查错误.形成良好但无效的日期:$date = '2015-02-30'; DateTime::createFromFor...

php – 绝对参数在DateTime :: diff中做了什么(DateTime [,bool absolute = false])【代码】

这是示例代码$c = new DateTime(); $o = clone $c; $o->modify('-60 days'); $diff = $c->diff($o); $diff2 = $c->diff($o, TRUE); var_dump($diff, $diff2);哪个输出object(DateInterval)#3 (8) {["y"]=> int(0), ["m"]=> int(1), ["d"]=> int(29), ["h"]=> int(0), ["i"]=> int(0),["s"]=> int(0), ["invert"]=> int(1), ["days"]=> int(60) } object(DateInterval)#4 (8) {["y"]=> int(0), ["m"]=> int(1), ["d"]=> int(29), ["...

PHP DateTime对象【代码】

为什么在第一个例子中我得到分钟和秒00:00,在第二个例子中,当没有指定小时时,我得到了实际时间?如果在创建对象的日期没有定义值,我想要始终为0.echo DateTime::createFromFormat('j.n.Y H', "3.4.2012 22")->format('Y-m-d H:i:s');输出:2012-04-03 22:00:00echo DateTime::createFromFormat('j.n.Y', "3.4.2012")->format('Y-m-d H:i:s');输出:2012-04-03 23:05:45 预计输出:2012-04-03 00:00:00解决方法:这将给你希望的格式:...

php – symfony从json数据持久化datetime【代码】

你好,我把数据作为JSON格式,在我的服务器网站上,我必须通过Doctrine存储它们.一切都很顺利,但当我收到日期时间格式数据时,我有验证错误.我在控制器中的分离动作上测试这种情况:public function indexAction($name) {$em = $this->getDoctrine()->getManager();$test = new Test();$test->setName("Test");//$test->setStart(new \DateTime());$form = $this->createForm(new TestType(), $test);$store = array("name" => "Test",...

php – DateTime对象在没有var_dump的情况下返回NULL【代码】

我在DateTime()类中遇到了一些奇怪的问题. 每次我试试这个:$object = new DateTime(); var_dump($object->date);var_dump()返回NULL,但如果我这样做:$object = new DateTime(); var_dump($object); var_dump($object->date);var_dump()给了我正确的答案,我不明白为什么.以前有人看过这个吗?解决方法:它与PHP被迫创建一个完整的对象表示形式有关,当您转储正在更新对象状态的整个对象时,而不是只访问一个单独的属性. 更重要的是,你...

php – 如何使用DateTime类将’02 / 31/2010’作为无效日期捕获?【代码】

我一直在PHP中使用DateTime类,因为date()具有Unix时间戳的缺点.但是,这两种方法都没有检测到没有31天的月份的无效日期,但是尝试使用第31天. 示例代码:try {$date = new DateTime('02/31/2018');$formattedDate = $date->format('Y-m-d'); } catch (Exception $e) { } echo $formattedDate;示例输出:2018-03-03更新1: 要使用checkdate(),我需要日期的组成部分.为此,我需要使用日期字符串作为构造函数来实例化DateTime对象.在’02...

php – strtotime和DateTime在解析一年时错误的一年【代码】

$year = date('Y', strtotime("2012")); var_dump($year);//returns 2013这是一个旧的服务器与PHP 5.2和一个新的PHP 5.4 服务器使用strtotime从2012-01-01或2012-01或2012这样的字符串中获取年份 我也尝试使用$dt = new DateTime(‘2012’)然后getTimestamp返回“1374516720”,即“Mon,22 Jul 2013 18:12:00 GMT” 是什么导致了这个错误?在documentation,它说strtotime只接受年份 我不知道该怎么办… 编辑:$year = date('Y', str...

php – 为什么DateTime :: diff在这些日期之间返回0天?【代码】

考虑以下测试用例:$start = new DateTime("2011-02-25"); $end = new DateTime("2011-03-25");$interval = $end->diff($start);echo "INTERVAL = ".$interval->format("%d"); // Should give me the // interval in days, right? echo "START = ".$start->format("Y-m-d"); echo "END = ".$end->format("Y-m-d");结果是:INTERVAL = 0 <---------- WTF???? START = 2011-02-25 END = 2011-03-25diff()似乎声称2011年2月25日至20...

php – 单元测试包含DateTime(“now”)的函数【代码】

我有一个类似这样的功能: public function getSomeInfo($id){$date_start=new DateTime();$day_of_week=$date_start->format("N");$date_start=$date_start->sub(new DateInterval("P".$day_of_week."D"));$date_end=new $date_start;$date_end=$date_end->add(new DateInterval("P5D"));$date_start=$date_start->format("Y-m-d");$date_end=$date_end->format("Y-m-d");$sql="SELECT * FROM `table` WHERE `table`.`id`=$idAND...

php – 错误的月份(二月) – DateTime :: createFromFormat【代码】

字符串的日期转换返回第2个月(2月)的错误值:$dtformat = 'Y-m-01'; $curDate = DateTime::createFromFormat('Y-m', '1996-02'); print_r($curDate); $dt = $curDate->format($dtformat); echo $dt."\n";而不是“1996-02-01”,它返回“1996-03-01”.这是$currDate数组:DateTime Object ( [date] => 1996-03-02 01:19:01 [timezone_type] => 3 [timezone] => America/New_York )所有其他月份工作正常.我在这里错过了什么? 谢...

php – new DateTime(‘2016-04-01 00:00:00’)返回’2016-04-01 12:00:00【代码】

这可能是一个明智的选择,但我无法看到解决方案.我有以下代码:$begin = new DateTime('2016-03-14 00:00:00'); echo $begin->format('Y-m-d h:i:s');输出是:2016-03-14 12:00:00我试图改变时区,设置时区,让它关闭.输出永远不会改变.是什么赋予了?我希望输出显示为“2016-03-14 00:00:00”.解决方法:您需要使用H原样24小时,h需要12小时. 12:00:00是12:00或00:00:00:$begin = new DateTime('2016-03-14 00:00:00'); echo $begin->...

在PHP中将DateTime字符串转换为不同的时区【代码】

好吧,我有以下代码$from = "Asia/Manila"; $to = "UTC"; $org_time = new DateTime("2012-05-15 10:50:00"); $org_time = $org_time->format("Y-m-d H:i:s"); $conv_time = NULL;$userTimezone = new DateTimeZone($from); $gmtTimezone = new DateTimeZone($to); $myDateTime = new DateTime($org_time, $gmtTimezone); $offset = $userTimezone->getOffset($myDateTime); $conv_time = date('Y-m-d H:i:s', $myDateTime->format(...

php – doctrine和Symfony 2中的DateTime字段【代码】

我对Symfony2很新,并设置了一个表单,通过doctrine将日期时间数据输入MySQL数据库,但是我收到以下错误:The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class DateTime. You can avoid this error by setting the "data_class" option to "DateTime" or by adding a view transformer that transforms an instance of class DateTime to scalar, array or a...

TIME - 相关标签