开发者

Set given time to 00:00:00 in PHP/MySQL

开发者 https://www.devze.com 2022-12-25 04:41 出处:网络
I\'m retr开发者_Go百科ieving a date from a MySQL database that shows up like this: \'2010-03-09 09:11:30\'

I'm retr开发者_Go百科ieving a date from a MySQL database that shows up like this: '2010-03-09 09:11:30'

How can I change the date to '2010-03-09 00:00:00'? I'm using PHP to perform the query. Thanks!


$time = '2010-03-09 09:11:30';
$timestamp = strtotime($time);
$zeroedtimestamp = mktime(0, 0, 0, date('n', $timestamp), date('j', $timestamp), date('Y', $timestamp));
echo date('Y-m-d H:i:s', $zeroedtimestamp);


Are you sure it is '2010-3-9 09:11:30' and not '2010-03-09 09:11:30'? If you really are getting the first from the DB, fix your schema fast.

Anyway...

A kind-of-ugly solution is:

$startOfDay = (substr('2010-3-9 09:11:30', 0, -8) . '00:00:00');


echo strtok(" ",$date).'00:00:00';

Why not to learn strings operations a bit? I's basic PHP knowledge as it's HTML text preprocessor. Most PHP operations is string parsing. It' really worth to learn some string functions

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号