Add N hours to MySQL datetime representation - php

How can I add N more hours to MySQL datetime representation? For example
Current: 2013-12-01 19:30:13 (or I can get it with date("Y-m-d H:i:s") in PHP)
How can I get this: 2013-12-01 22:30:13 (adding 3 more hours)?
date("Y-m-d H:i:s") + 3 isn't working in PHP

in PHP:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours');

In MySQL you can use date_add
From the docs:
DATE_ADD(date,INTERVAL expr unit)
These functions perform date arithmetic. The date argument specifies
the starting date or datetime value. expr is an expression specifying
the interval value to be added or subtracted from the starting date.
expr is a string; it may start with a “-” for negative intervals. unit
is a keyword indicating the units in which the expression should be
interpreted.
The INTERVAL keyword and the unit specifier are not case sensitive.
For your case you can do:
date_add(now(), interval 3 HOUR)
sqlfiddle demo

It depends on where you want to do it.
in mysql
SELECT NOW() + INTERVAL 3 HOUR
SELECT CAST('2013-12-01 23:49:09' AS DATETIME) + INTERVAL 3 HOUR
in php
Date("Y-m-d h:i:s", time() + 3600 * 3)

Related

How to deduct time in php 24 hours format

How to subtract 00:00:00 example with 1 hour?
I've tried:
$minus1=strtotime(00:00:00)-strtotime('-1 hour');
but instead of an answer of 23:00:00, I got -1502671524 instead.
You can use DateTime and DateInterval option to do in PHP.
$date = new DateTime('00:00:00');
$date->sub(new DateInterval('PT1H00M'));
echo $date->format('H:i:s') . "\n";
The P stands for Period. The T stands for Timespan. H stands for Hour to reduce and finally M stands for Minutes to reduce.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.

Date('now') in PHP

After a long time I needed to use date function of PHP. I wrote something like:
echo date('now');
and I got the output below:
1220123
What does that mean ?
From the PHP manual :
n Numeric representation of a month, without leading zeros
o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that
year is used instead. (added in PHP 5.1.0)
w Numeric representation of the day of the week
So, date("now") displays 12 (n), 2012 (o) and 3 (w).
You're probably looking for :
date("Y-m-d") for a date
date("Y-m-d H:i:s") for a datetime
"now" is not a valid parameter for for this expectation, infact it should be strtotime function here, not date.
Date considers your now as
n
Numeric representation of a month, without leading zeros
o
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
w
Numeric representation of the day of the week
you need to give a valid format to date function (not recognize the 'now' string as meaning of now )
$date = date("Y-m-d H:i:s");
or you can use the DateTime class
$date = new DateTime();
Seems you consider "now" as a word to get the current date and time, however it would compile on each character. Here is the explanation how it'll compile.
n = Month in number
o = It considers as a year in ISO-8601.
w = Week in number
So that's why it's returning you the date, year and number of week in a month.
Hope I can explain you bit easily.
"now" is not a valid parameter for date()
Correct syntax to print current date in
yyyy-mm-dd hours minutes seconds
format is as given below
echo date('Y-m-d h:i:s');
also see PHP manual for details of date() function
http://php.net/manual/en/function.date.php

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

Localize output of strtotime string

I read through manuals concerning strtotime and strftime as well as related functions, but I cannot seem to make any progress in trying to solve my problem.
Basically, I'm interested in ways to print output of $deldate5 in local language (Dutch in this case).
$deldate5 = date("d.m.Y., l", strtotime("today + 5 day", time()));
I would most likely need to ditch strtotime string and replace it with something else in order to facilitate "today + 5 day" parameter.
Can anyone help? Thank you in advance.
Let's pick this apart:
$deldate5 = date("d.m.Y., l", strtotime("today + 5 day", time()));
This is doing two things:
strtotime: Create a UNIX timestamp (number of seconds since the epoch).
date: Format the output.
Your problem is related to the output (2.), not creating the timestamp (1.). So let's put this apart:
$timestamp = strtotime("today + 5 day", time());
$formatted = date("d.m.Y., l", $timestamp);
The only thing required now is to deal with the following line of code:
$formatted = date("d.m.Y., l", $timestamp);
The formatting parameters for the dateDocs function are:
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
l - A full textual representation of the day of the week
As l (lower case L) requires a locale in your output, let's see which formatting parameter strftimeDocs has to offer that is similar:
%A - A full textual representation of the day.
So it's just a small step to change from date to strftime:
$formatted = strftime("%d.%m.%Y., %A", $timestamp);
Hope this helps.
Try setting the date default timezone to your local timezone:
http://www.php.net/manual/en/function.date-default-timezone-set.php

How to get delay of 30 sec in current time in php?

Date("F d Y H:i:s") gives current time in php. How can I get delay of 30 secs and 1 min in current time using php, not sure but something like Date ("F d Y H:i:s-30") or Date("F d Y H:i-1:s") ?
date("F d Y H:i:s", time() + 90)
date has a second, optional parameter: the timestamp to convert to a string. time returns the current timestamp, which is just an integer, so you can add 90 to it to get the time 90 seconds from the current time.
You mean format the time for 1:30 in the future?
date('format string', time()+90);
In the above, 90 can be any positive or negative offset.
In PHP, dates and times are generally worked with as timestamps ; which are a number of seconds since 1970 or so.
To get the current timestamp, you can use time()
And to format it, you use date(), as you already know.
So, for current time plus 1 min and 30 seconds (ie, 90 seconds) :
date('F d Y H:i:s', time() + 90);
Or, as an alternate way (a bit more fun ? ), you can use strtotime() :
echo date('F d Y H:i:s', strtotime('+1 minute +30 seconds'));
OK, more complex in this case, I admit ^^
But, in some other situations, it's good to know it exists ;-)
Use this function:
date("F d Y H:i:s",time()-90)

Categories