首页 \ 问答 \ datetime函数在PHP中(datetime function in php)

datetime函数在PHP中(datetime function in php)

我想将数据和时间存储在我的MYSQL数据库中。我在我的数据库中有一个日期时间字段我想存储当前数据时间在我的数据库如何让我得到当前日期时间?如何通过SQL查询传递给数据库

那我该如何检索以正确的yyyy - dd - mm格式或任何其他格式打印它什么将会是时间格式? 将是23小时等? 我如何打印日期和时间?


I want to store the data and time in my MYSQL db.I have a datetime field in my db I want to store current datatime in my db How shold i get the current date time?How to pass it to db via a sql query

How can i then retriev an print it in correct yyyy--dd--mm format or any other format What wil be format of time? wil it be 23 hrs etc? How do i print date and time?


原文:https://stackoverflow.com/questions/1483585
更新时间:2019-07-12 10:31

最满意答案

假设名为'items'的表和名为'modified'的'timestamp'类型的字段

$r = mysql_query("INSERT INTO items (modified, x, ...) VALUES (CURRENT_TIMESTAMP, $x, ...)");
// (or "UPDATE items SET modified=CURRENT_TIMESTAMP, x=$x, ...)
...

$r = mysql_query("SELECT UNIX_TIMESTAMP(modified) FROM items");
$item = mysql_fetch_assoc($r);
$formatted_ts = date('g:ia', $item['modified']); // or another format *

你需要添加适当的错误检查,我省略了; 还需要根据时间区域进行调整,这也是我所忽略的


Assuming table named 'items' and field named 'modified' of type 'timestamp'

$r = mysql_query("INSERT INTO items (modified, x, ...) VALUES (CURRENT_TIMESTAMP, $x, ...)");
// (or "UPDATE items SET modified=CURRENT_TIMESTAMP, x=$x, ...)
...

$r = mysql_query("SELECT UNIX_TIMESTAMP(modified) FROM items");
$item = mysql_fetch_assoc($r);
$formatted_ts = date('g:ia', $item['modified']); // or another format *

you'll need to add appropriate error-checking which I've omitted; also need to adjust for consideration of timezones, which I've also left out

2009-09-27

相关问答

更多

PHP新DateTime vs date()(PHP new DateTime vs date())

您需要使用format方法来打印DateTime对象的日期。 $con = connectToDatabase(); $now = new DateTime; $filePath = 'wipe-log.log'; $file = fopen($filePath, 'a+'); $log = "\n\n" . $now->format("F d Y H:i:s"). " ----------------- \n\n\n"; fwrite($file, $log); 时间和日期格式与您在日期功 ...

DateTime的继承是否会更改任何默认函数行为?(Does inheritance of DateTime change any default function behavior?)

实现/扩展类时,父类(在本例中为DateTime )将其方法传递给子类( OwnDate ),除非您覆盖它们。 因此, DateTime的方法仍然可以像直接使用DateTime类一样运行。 When you implement/extend a class, the parent class (in this case DateTime) will pass its methods to the child class(OwnDate), unless you override them. Erg ...

PHP检测列值是DATE还是DATETIME(PHP detect if column value is DATE or DATETIME)

您可以使用preg_match作为 function decodeTimestamp($var) { if(preg_match('/\d{4}-\d{2}-\d{2}$/',$var)){ return date("d.m.Y", strtotime($var)); }elseif(preg_match('/\d{4}-\d{2}-\d{2}\h\d{2}:\d{2}:\d{2}$/',$var)){ return date("d.m.Y H ...

PHP进程()函数(PHP process() function)

在示例#2的顶部,他们正在定义该功能。 function process($var1, $var2, $farr) { foreach ($farr as $f) { echo $f($var1, $var2) . "\n"; } } At the top of example #2, they are defining that function. function process($var1, $var2, $farr) { foreach ($far ...

strtotime()DateTime类模拟方法(strtotime() DateTime class analog method)

这将是DateTime::__construct : $date = new DateTime('Sunday'); That would be DateTime::__construct: $date = new DateTime('Sunday');

如何在PHP中检测模糊和无效的DateTime?(How to detect Ambiguous and Invalid DateTime in PHP?)

我不知道有任何现有的实现,我还没有理由使用这些先进的日期/时间功能,所以这里是一个洁净室的实现。 为了启用问题中说明的语法,我们将扩展DateTimeZone ,如下所示: class DateTimeZoneEx extends DateTimeZone { const MAX_DST_SHIFT = 7200; // let's be generous // DateTime instead of DateTimeInterface for PHP < 5.5 pub ...

使用xpath PHP过滤xml datetime属性(Filter xml datetime attribute with xpath PHP)

你可以使用XPath来代替遍历所有的<Valores> : $filtered = $mypix->xpath( '//Valores[contains(@Data,":30:00")]' ); foreach( $filtered as $valores ): (...) endforeach; 上面的XPath是不言自明的:它搜索包含子串“:30:00”的类“数据”的<Valores>节点。 阅读更多关于SimpleXMLElement :: xpath的信息 了解更多关于XPath的信息 ...

调用PHP函数(Call PHP function)

我的建议是使用数组而不是使用参数的数量,例如你的函数调用应该是这样的。 $params[6] = 'value'; MyFunction($params); 用于识别第六个参数已设置 function MyFunction($params){ If ( isset($params[6]) ) // parameter six has value } 我希望它将成为另一种方式 My suggestion is use array instead of using number of arg ...

相关文章

更多

最新问答

更多
  • Unity着色器错误;(Unity shader error; presumably in if statement)
  • 如何在Liferay portlet中设置Cookie?(How to set a Cookie in Liferay portlet?)
  • C#只读访问List <>或其他集合,或ToArray()魔术(C# readonly access to List<> or other collection, or ToArray() magic)
  • 如何使用Google Web工具包创建登录应用程序?(How to create a login application using Google web tool kit? [closed])
  • 如何使我的函数返回结果作为全局变量?(How to make my function return results as a global variable?)
  • 为什么我的网站需要“启用32位应用程序”?(Why does my website need “Enable 32-bit applications”?)
  • 红宝石数组具有相同的值(ruby array of hash with same value)
  • Android(在Scala中):StackOverflowError取决于何时启动线程?(Android (in Scala): StackOverflowError depends on when to start a thread?)
  • 适用于iOS应用的通用链接(Universal Links for iOS apps)
  • 创建并打开文件linux编程(create and open file linux programming)
  • Datepicker,第2个日期是从第1个日期开始的X天(Datepicker, 2nd date is X days from 1st date)
  • 背景大小过渡不起作用(Transition on background-size doesn't work)
  • 在React / Redux中的On Click函数中传递Prop(Passing a Prop in an On Click Function in React/Redux)
  • 关键字在代码中做了什么,是否有没有此关键字的替代方法?(what does the keyword this does in the code and are there any alternate methods without this keyword? [duplicate])
  • 反向设计FoxPro / dBsae数据库以创建EER模型(Reverse Engineer a FoxPro/dBsae database to create the EER model)
  • 在R中的变量中组合具有相同值的行(Combine rows that have same value in a variable in R [duplicate])
  • SQL Server:非空唯一主键(SQL Server: Non-null unique vs. Primary Key)
  • 将扩展ASCII字符代码转换为ISO-8859-1(Convert extended ASCII character codes to ISO-8859-1)
  • 如何在.NET中读取m4a文件中的标签?(How to read tags out of m4a files in .NET?)
  • 呼叫链接的性能提升?(Performance gain on call chaining?)
  • 风景名胜区规划设计文本里面都包含哪些内容?推荐一家旅游规划设计公司?谢谢!
  • 如何在String中将字符串转换为枚举?(How do I convert a string to enum in TypeScript?)
  • SQLite SELECT出现异常如何解决它?(SQLite SELECT gives exception How to fix it?)
  • NSLog不会从ViewController类输出,而是来自AppDelegate类的WILL。(NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class. iOS:Objective C)
  • 更改UIPicker突出显示的标签宽度(change UIPicker highlighted tab width)
  • 空心倒五角形(Hollow inverted pentagon)
  • 如何设置仅使用OpenID的Plone站点(How to setup Plone sites working only with OpenID)
  • Paperclip缺少Amazon S3的协议(https)(Paperclip is missing the Protocol (https) with Amazon S3)
  • 从宏中部署netbeans中的ANT文件(Deploy ANT file in netbeans from macro)
  • XMLReader是未知的(XMLReader is unknown)