subtract a few hours from a date in php - 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');

Related

Php date() function add extra day

I'm trying to echo out the current system date. But it outputs the date of tomorrow. I double check the System Date and tested changing the date. Still it adds 1 extra day when echoing out.
What could be the problem?
<?php
echo date("Y-m-d");
?>
If not specified, 2nd argument for date() defaults to current Unix timestamp - amount of seconds since 01.01.1970, 0:00 GMT.
You can use this to set your timezone, or manually convert the date to correct one: date($format, time() + $difference).

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

Removing hh:mm:ss from PHP's strtotime function

I'm setting up a HighCharts line graph and the x axis labels won't match up with the data points. The data points roll up to the whole day (no hours). When I add the pointStart, though, I'm getting the unix timestamp for when I run it:
pointStart: <?php echo strtotime("-1 month -1 day") * 1000; ?>
Can I say "give me the unix timestamp for a month and a day ago at midnight" without complicating things?
Of course you can :
<?php echo strtotime(date('Y-m-d', strtotime("-1 month -1 day"))); ?>
The date() format will remove hours minutes and seconds, then you can re-get your timestamp

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

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");

Compare timestamp to date

I need to compare a timestamp to a date. I would just like to compare the date portion without the time bit. I need to check whether a timestamp occurs on the day before yesterday i.e. today - 2.
Could you show me a snippet please? Thank you.
I've been reading through the PHP docs but couldn't find a very clean way of doing this. What I found was converting the timestamp to a date with a particular format and comparing it to a date which I get by doing a time delta to get the date before yesterday and converting it to a particular format. Messy.
You can arcieve this by using the function strtotime.
To round to a day I personaly like to edit the timestamp. This is a notations of seconds since epoch. One day is 86400 seconds, so if you do the following caculation:
$time = $time - ( $time % 86400 );
You can convert it back to a date again with the date function of PHP, for example:
$readableFormat = date( 'd-m-Y', $time );
There is also much on the internet about this topic.
you can use the strtotime function
<?php
$time = strtotime("5 june 2010");
$before = strtotime("-1 day",$time);
$after = strtotime("+1 day",$time);

Categories