Strtotime not showing exact time

Please house am using strtotime in php to update my mysql date column that has Datetime as its format, but the out put am getting from the strtotime only shows year, month and day correctly, but the time is left to 12:00:00 instead of giving me the current actual date and time.
If i use echo date(“Y-m-d H:i:s”); it gives me what i want but i do strtotime like this

$today = strtotime('today');
$todays = date("Y-m-d h:i:s", $today);
echo $todays;

i get this in return 2020-09-13 12:00:00
instead of 2020-09-13 20:29:12

Please how can i get the time too using strtotime?, i need to use strtotime as to enjoy this features of +next month, -last week and so on that strtotime makes available

I think, the problem is incorrect timezone. Try to set yours.

https://www.php.net/manual/en/datetimezone.construct.php
https://www.php.net/manual/en/function.date-default-timezone-set.php

if i do

$red = date('Y-m-d H:i:s', strtotime("+2 Months"));
echo $red;

I get the real month with exact time, but if i want to get current time the time is not shown

Another one idea - try strtotime('now'); instead of strtotime('today');

1 Like

And if i do

$red = date('Y-m-d H:i:s', strtotime("Now"));

I will get 2020-09-13 20:40:45 but i know that is Y-m-d H:i:s is being outputted as the strtotime does nothing in getting Now time or current time

I tried it but it seems Y-m-d H:i:s is the one being shown, because even if i remove Now and use meaningless word in the strtotime function i will still get the correct date and time, so it seems the date() is the one working not the strtotime, as the strtotime only works when +1 Day or +1 Week is added to it

Sorry, this works. thanks alot @igor_g

// if i do 
date('Y-m-d H:i:s', strtotime("ujhgh")); 
// i got this 1970-01-01 00:00:00 

// But if i do 
date('Y-m-d H:i:s', strtotime("Now"));
// I got this 2020-09-13 20:51:06

And thats exactly what i wanted, Thanks for coming by @igor_g

Please, using this strtotime how do i delete all columns in database that is past or one day order than current date time in the date column?

If i run

$old = date("Y-m-d H:i:s", strtotime('-1 day'));
delete table list WHERE date = $old

This delete sql may only work if the $old matchs the exact year month day hour minutes seconds in the table which not going to be possible unless you are runing the delete function check every secs, and if a seconds is mixed the row stays there forever.
So i want to know how to run it like this if current date is greater than old date in mysql or php?

And secondly anytime i manually edit the date column in the database which is a datetime data type in sql it becomes adds 0000 at the end which are milliseconds and i don’t want it, how can i stop it, as it makes my table look dirty and odd

What about deleting them if they are less than current date/time minus a day? Does that work?

delete table list WHERE date < $old

I believe you can do that in SQL, without having to use PHP to create your “yesterday” date, but I’m not sure how. You can research that.

https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_adddate

1 Like

So for clarity

strtotime(‘today’) means “Today at midnight.” (or specifically, it’s a DATE format - so the hour, minute, and second, are all 0s.)

Depends on the database engine. In MySQL, it would be

DELETE FROM table WHERE `date` < NOW() - INTERVAL 1 DAY

Or, if you really wanted to do it in PHP, time() - 86400 gets you your value (assuming there hasnt been a leap-second in the past day.), since you want to work in timestamps.

2 Likes

Thanks @m_hutley
You saved me some headaches.

However how can i remove milliseconds from date and time column in mysql anytime i manually edited the column value, it leaves me with extra 4 zeros after secs

You have defined your column with fractional seconds enabled (DATETIME(4)). Redefine the column as not having fractional seconds (DATETIME(0)).

(The number, for the record, is the number of points of precision in the fractional seconds. See the manual reference 11.2.6 Fractional Seconds in Time Values

Really, i don’t know how i did it because i didn’t use sql command in creating the table, i used normal add column in phpmyadmin.

Please how do i set it back to zero from the column change interface in phpmyadmin?

Set the “Length/Values” of the DATETIME column to 0.

Am still having the six zeros in my column after i set the length value to 0, if i post date to it using php insert or update it looks Normal like this 2020-09-14 20:25:12

But if i go straight to the mysql myadmin and manually edit any column date in a given row as to change its value i end up with 2020-09-14 20:25:12.000000

If i manually edit the value and set it to 2020-09-14 20:25:12 it will automatically append six zeros to it,because during the manual edit there is a calander field which has milliseconds in it in a slider range which is extreme is zero. But i don’t want to use milliseconds, so why is it appending zeros and is there a way to remove that milliseconds scroll bar from the calander date field that is shown when editing values?

Out of interest, will you be entering data on your live site using phpmyadmin, and displaying it without formatting it? If not, does it matter what the database is storing internally? Just don’t display the decimal part.

It will affect my functions in php, database is not just about storing informations, yes there could be times you want to manually edit a users payment date or some thing which the person don’t have to resend the script via php. So admin helps in doing the adjustment.

Secondly, i pull dates from mysql and i use it in computing other function’s in php.
Now if i bring that data to do something like
If date is greater than 5mins then session expires or terminate a function.

Now my method of archiving this is
I created a function that stripes off - and : and empty space from the returned date and then use date function in this formats

$fromquery = mycustomstrip($result['date']);
$now = date('YmdHis');
$count = $now - $fromquery;

If ($count > 300) {
// say or do something.

If i try such code above from a date with 6 zeros is almost minus one billion from million. Even if you use it in a strtotime function it may behave differently even though am yet to try it.
I see no reason why mysql will force someone to use milliseconds.
Even if you want to insert new data using mysql insert tab in phpmyadmin you will face same 6zeros