PHP time() changes hours and seconds but not minutes? - php

I'm trying to output the current date and time using this code:
$theDate = date('y-m-d H:m:s', time());
echo $theDate;
And it works fine but the output for time does not change minutes, it simply sites at HH:07:SS, so the minute sits at 07 and the only thing that changes is the seconds and hours.
Is this because of the time function inside PHP? Does it only update minutes so often? Why would it not update the minutes as well?
How can I get an output the same but with minutes showing correctly?
Whenever i run strftime on the server it outputs fine, just trying to figure it out above.

Use i not m:
$theDate = date('y-m-d H:i:s'); echo $theDate;
07 is July :)

m represents months, not minutes. You need to use i for minutes. See the date() manual page for more information.
$theDate = date('y-m-d H:i:s'); echo $theDate;

Your format string is wrong.
It could be: y-m-d H:i:s

You dont need to give date function the second parameter time().
Just try date("format string");

Related

Change time from 12 hour format to 12 hour AM/PM format

I've set the timezone on my website pages as such:
date_default_timezone_set("America/Los Angeles");
I've declared a time variable as such:
$time = date("h:i:sa");
then adding it to the database. I then retrieve the time from the database like such, trying to reformat it:
$time2 = date("h:i A", strtotime($row['Time']));
and then printing out the time:
echo $time2;
This method, however, messes up AM and PM. If $time was added to the database at 11:59:00 at night, for example, it will print out 11:59 AM, not 11:59 PM. I think the issue is that when the time is added to the database, no clear distinction is made between AM and PM. However, I'm still unsure how to fix this. I've tried adding the time with the AM PM already in it, like such:
$time = date("h:i:sa A");
and then accessing it like such:
$time2 = $row['Time'];
but this still doesn't fix the bug. Is there a way around this while keeping my 12 hour format? Or do I have to change to 24 hour format to get this to work?
Please start storing dates/times in Y-m-d/H:i:s format in DATETIME, DATE or TIME fields. Your life, and ours, would be much easier.
Until then, use this PHP solution:
$dt = DateTime::createFromFormat('!h:i:sa', $row['Time']);
echo $dt->format('H:i:s');
demo
or mysql solution:
SELECT STR_TO_DATE('08:46:07am','%h:%i:%s%p')

subtract a few hours from a date in php

I am trying to echo the timestamp for a file using this:
Last updated: <?= date("m/d/Y H:i:s",filemtime("file1.html")) ?>
but this is on a server that is 6 hours ahead.
I've tried using DateTime::sub or date_sub or sub but none of these are being recognized. Do I need to call the date class or something?
filemtime returns a timestamp, just subtract the number of seconds you need.
date("m/d/Y H:i:s",filemtime("file1.html")-6*3600)
or specify the timezone
$time = new Datetime(new Datetimezone('America/Chicago'));
$time->setTimestamp(filetime("file1.html"));
echo $time->format('m/d/Y H:i:s');

how to convert time() output to a date in php [duplicate]

This question already has an answer here:
Converting facebook time to human readable time with PHP
(1 answer)
Closed 9 years ago.
I am using time() command in php to store a timestamp value for every time database is updated i.e whenever a value is updated in database a timestamp is added in a time.
For eg: when a change was made to database yesterday night, the value added was 1368132319.
I know the the time() commands returns the no of seconds elapsed from jan 1 1970.
Now what i want to do is convert these no of seconds into a user understandable form which can be displayed on an html page. Like these seconds are converted to date and time.
How do i do that? i cannot think of a logic to implement it. googled it but to no avail
Pretty simple thing, simply use datetime
$date = date_create();
date_timestamp_set($date, 1171502725);
echo date_format($date, 'U = Y-m-d H:i:s') . "\n";
All in the manual http://php.net/manual/en/datetime.settimestamp.php
you can see the demo link....
<?php
$time_in_seconds = 1368132319;
$format = 'Y-m-d H:i:s';
echo date($format,$time_in_seconds);
?>
You can use the date() function. Example:
$timestamp = 1368132319; // in your case the value from the DB
echo date('Y-m-d', $timestamp); // 2013-05-09
echo date('Y-m-d H:i:s', $timestamp); // 2013-05-09 23:45:19
simply you can use below
<?
echo date("D-M-Y",$strtime);
?>
there are various format available for DATE function in PHP you can use required one.

PHP date() doesn't work?

I am trying to add 1 minute to the current time:
echo date("Y-m-d H:m:s", strtotime("+60 seconds"));
echo '<br />';
echo date("Y-m-d H:m:s");
The output is in both cases:
2012-09-02 17:09:02
2012-09-02 17:09:02
Which is obviously wrong (in both cases - the current time is in my country 1AM).
What I am doing wrong? I have saved into the datetime column the value 2012-09-02 17:09:38 and now I try to add to the current time 60 seconds and then if the difference between stored datetime and the current datetime is less than 60s, then I want to inset another row in the database...
But the problem is, the date() function displays weird output.
("Y-m-d H:i:s");
not
("Y-m-d H:m:s");
m is month, it can't be minutes as well
date("m") will show the current month numerically. So this is why your +60 seconds does not work. Use date("Y-m-d H:i:s")
Why the current time is wrong could be several reasons. Probably something with time or timezone settings on your server.
As far as timezone goes, this might help you: date_default_timezone_set

In the format of date and time in php, the timing is not correct

When I am fetching the date n time using this function:
date("d-M-Y h:i A");
It gives the wrong timing.
The current correct timing is 04.30 pm but my function is showing 11.00 am.
That means showing 5.30 hours early.
help me out to fetch the correct timing.
Going by your username, I think you're from India.
So put date_default_timezone_set('Asia/Kolkata');
right before date("d-M-Y h:i A");
OR
$offset= strtotime("+5 hours 30 minutes");
$date = date("Y-m-d H:i:s",$offset);
There are two probable causes.
1) The timezone.
2) The server time.
These are actually related.
Check the server time (ie the system time on the machine running the code). If it's incorrect; fix it. If it's correct, then check the timezone also and reference it against the default timezone on the php side.
If working on localhost, change your time zone in your php.ini file.
OR try folowing way:
<?php
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
echo $date->format( 'H:i:s A / D, M jS, Y' );
?>
It seems that you are getting the UTC time instead of your timezone (which is +5:30 from GMT). (This could be because your default timezone could be set to UTC)
Try this:
date_default_timezone_set('GMT');
$temp= strtotime("+5 hours 30 minutes");
$date = date("Y-m-d H:i:s",$temp);
echo $date;

Categories