Add 1 hour on mysql timestamp in php - php

In my Mysql table I have a timestamp field.
In my php page I show the record ($row[data]) in this way
echo date("d-m-Y H:i:s", strtotime($row['data']));
Now in my php I'd like to add 1 hour on my date.
How can I do this?
Thanks

echo date("d-m-Y H:i:s", strtotime($row['data'])+3600);
note that you can't compare resulting dates in "d-m-Y" format.
either compare strtotime results or initial dates. or format your results into proper format first.

You could do this straight away in MySQL:
SELECT `date` + INTERVAL 1 HOUR AS `date`
FROM ...

echo date("d-m-Y H:i:s", strtotime($row['data'].' +1 hour'));

Try with:
strtotime($row['data'] . ' +1 hour')

echo date("d-m-Y H:i:s", strtotime($row['data']) + 3600);
Just add the time manually (3600 seconds = 1 hour).

Add 3600 seconds and you will be fine.
echo date("d-m-Y H:i:s", strtotime($row['data'])+3600);

Related

24 hour format for the actual time?

I would like to get the actual date + time, and convert it (the time) to a 24 hour format (e.g: 15/04/2017 13:32:02)
My current code is :
date_default_timezone_set("Asia/Magadan");
$date = date("d/m/Y h:i:s");
echo $date;
That display :
15/04/2017 01:32:02
Thanks.
Put the h capital for 24 hour format date("d/m/Y H:i:s");
Finally, to change to a 24 hour format we only have to change the H of the hour to a MAJ/CAPS.
$date = date("d/m/Y H:i:s");
http://php.net/manual/en/function.date.php
convert (h:i:s) to (H:i:s) . that's worked for me

PHP - strftime (minutes and seconds ignored)?

I tried to save time in format YYYY-mm-dd 23:59:59 to mysql database column with datetime. I don't understand why minutes and seconds are ignored always 00 ? Thank you very much for help.
PHP:
$time = strftime('%Y-%m-%d %H:%i:%s', time());
Output:
2014-07-16 11:00:00
You can simple use:
$time = date("Y-m-d H:i:s");
or even better use mysql NOW() function
you have to specify timezone as follows.
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions.
you can use mysql function
now()
You are looking for
$time = strftime('%Y-%m-%d %H:%M:%S', time());
from the documentation
%S Two digit representation of the second
%M Two digit representation of the minute 00 through 59
Also, what PHP version are you using? %i:%s should not be 00:00

One year back query datetime

I want to get all records from now between a year ago but it's not working exactly.
Made in CodeIgniter
This is my query , antything wrong with it ? :
$currentDate = date("Y-m-d H:m:s");
$yeardate=date('Y-m-d H:m:s', strtotime('-1 year'));
$this->db->select('TimeStamp');
$this->db->where('Transaction', 'COMPLETED');
$this->db->where('TimeStamp>=',$yeardate);
$this->db->where('TimeStamp<=',$currentDate);
$query = $this->db->get('R_Logs');
$results = $query->result();
Thanks in advance
I forgot to add a space between TimeStamp <= and TimeStamp >= .
My bad.
I think it might be easier to try Epoch time and then query off of that - e.g. Get epoch for a specific date using Javascript
That will convert the date to number of days since January 1, 1970. then from that you can subtract 365 days ( or use the yearInteger %4 trick to find out if it is a leap year)
Change these two lines any try once:
$currentDate = date("Y-m-d H:i:s");
$yeardate = strtotime(date('Y-m-d H:i:s') . ' -1 year');
Dump your query and debug:
echo $this->db->last_query();

Print the time an hour ago [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Given a time, how can I find the time one month ago
How can I print an hour ago in PHP using Date?
$date=date("Y-m-d H:i:s");
$time(-1, now);
$result=$date.$time;
So If I wanted to say "John visited last "
Would print
John visited last 20th Feb 2012, 17.26
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
$date = date("Y-m-d H:i:s", time() - 3600);
time() -> Current timestamp
Time minus 3600 seconds, is the time 1 hour ago. To get the date formatted, you can look here for the options: http://php.net/manual/en/function.date.php
Alternatively you could use the following format:
$date = date("Y-m-d H:i:s", strtotime('-1 hour'));
Though using that method can be a little clunky if you want to remove more specific units of time (Such as one day and 3 hours).
Thats if I've understood what you want to do correctly that is.
I suppose you would be fetching date and time from mysql and the best thing to do is using mysql's DATE_FORMAT function and work out.
Other wise in simple php you could do it like this
$date=date("Y-m-d H:i:s", $time -3600);
Better option is to use strtotime like this one
$date=date("Y-m-d H:i:s", strtotime('-1 hour'));
And get the work done.
Mmm, search the manual the function I used. You are missing something about PHP date/time functions...
// Get the date string for time() - 3600, that is
// the current time minus 3600 seconds (= 1 hour)
$date = date("Y-m-d H:i:s", time() - 3600);
$result = $date;

How to get next Date with specific time in PHP

I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00".
the code i was using get to do that,
<?php
$next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00";
$new_trig_time_stamp = strtotime($next_day_date);
$trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
echo $trigger_date_time;
?>
the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00".
Is there any other way to do so.
When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)
$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
strtotime() is very useful here.
$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );
Calculate it via unix timestamp - much less annoyance
<?php
$trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24);
echo $trigger_date_time;
?>
echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';
Have a look at date_add.
In more detail, something like...
$myDate = new DateTime("Some time");
$myDate->add(new DateInterval("P1D"));
You can then use $myDate->format(…) to extract formatted representations.

Categories