I would like to subtract 180 days from a GMDATE() (for UTC purposes) I have in a PHP script. I have just looked on SO but hardly anybody is using gmdate().
This is what I have:
$from_date = gmdate("Y-m-d H:i:s");
Is there a simple way to subtract X amount of days from this, without using a different function completely? Or should I be doing it another way?
Use DateTime. Much better for working with dates especially when working with timezones.
$datetime = new DateTime(null, new DateTimeZone('UTC'));
$datetime->modify('-180 days');
echo $datetime->format('Y-m-d H:i:s');
See it in action
Reference
DateTime
DateTimeZone
Formatted in UTC should be like;
$timezone = new DateTimeZone('UTC');
$datetime = new DateTime('NOW', $timezone);
$datetime->modify('-180 days');
echo $datetime->format('Y-m-d H:i:s');
Datetime: PHP DateTime class
public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
Timezone: PHP DateTimeZone class
public __construct ( string $timezone )
gmdate('Y-m-d H:i:s', time() - 15552000)
Related
I'm looking for an elegant/efficient way to take out the time portion of a datetime in format 'Y-m-d H:i:s', and then add 3 days.
Currently the solution is:
date('Y-m-d 00:00:00', strtotime("+3 days", strtotime('2017-01-23 05:32:12')));
where 2017-01-23 05:32:12 is the date, and this correctly outputs 2017-01-26 00:00:00.
It just feels like there has to be a better way to do this.
Thanks
DateTime() offers several of ways to do this. None of them are any less verbose than your current method:
// Plain old DateTime()
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');
// DateTme using DateInterval to add three days
$date = (new DateTime('2017-01-23 05:32:12'))->add(new DateInterval('P3D'))->format('Y-m-d 00:00:00');
// DateTime setting the date to midnight instead of using 00:00:00
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->modify('midnight')->format('Y-m-d H:i:s');
If the date is today you can shorten this a bit:
$date = (new DateTime('+3 days'))->format('Y-m-d 00:00:00');
you can use DateTime class
$date = new DateTime('2017-01-23 05:32:12');
$date->modify('+3 days');
$outputDateString = $date->format('Y-m-d 00:00:00');
one line:
$date = ( new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');
I have a question. I try to use datetime in php.
I did :
$now = new \DateTime();
When I print_r the $now I have :
DateTime Object
(
[date] => 2016-12-01 05:55:01
[timezone_type] => 3
[timezone] => Europe/Helsinki
)
When I look at clock I have 16:05. I need to set the timezone ? I want to use Bucharest timezone. How I can get the right date and hour ? Thx in advance
You have two ways to set right timezone. It is object way and procedural way.
Examples
Object
$datetime = new DateTime();
$timezone = new DateTimeZone('Europe/Bucharest');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');
Procedural
date_default_timezone_set("Europe/Bucharest");
$date = date('F d, Y H:i');
echo $date;
Manuals
PHP: date
PHP: DateTime
PHP: DateTimeZone
Update
Check code below, may it will work for you:
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime();
$timezone = new DateTimeZone('Europe/Bucharest');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');
?>
There are examples in the manual, you can set the timezone on the instantiation of the DateTime class like this
$now = new \DateTime('now', new DateTimeZone('Europe/Bucharest'));
put this line of code above your script:
date_default_timezone_set('Europe/Bucharest');
<?php
$datetime = new DateTime( "now", new DateTimeZone( "Europe/Bucharest" ) );
echo $datetime->format( 'Y-m-d H:i:s' );
Demo repl.it
You can use setTimezone() method of DateTime class to set the timezone to Europe/Bucharest, like this:
$now = new \DateTime();
$now->setTimezone(new DateTimeZone('Europe/Bucharest'));
Here's the reference:
http://php.net/manual/en/datetime.settimezone.php
I need to set timestamp eg. 4 hours ahead and 2 hours ahead separately
In my database, I have their columns as timestamp.
I know I could do something similar to this but am not sure if it's correct.
// For 4 hours ahead of time
$dt2 = date("Y-m-d 04:i:s");
//For 2 days ahead
$dt2 = date("Y-m-02 H:i:s");
//For 4 hours ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+4 hours'));
//For 2 days ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+2 days'));
In my mind it's much better to work with DateTime field and the DateTime class.
You have the ability so modify that objects very easily. For example:
$aktDate = new \DateTime();
Now you have the actual date and time in an object. If you want you can put a string insight the DateTime function so set your date manually.
$aktDate = new \DateTime('Y-m-d 04:i:s');
Not you can modify your dates if you want with the modify function.
in your case:
$pastDate = clone $aktDate;
$pastDate->modify('+2 days');
$futureDate = clone $aktDate;
$futureDate->modify('+4 days');
if($pastDate < $aktDate && $aktDate < $futureDate) {
// do something
}
I like the DateTime function much more because it's readable and you can work directly with your DateTime fields from your MySQL database if you have such fields. You can write that example much shorter but so you have better readability.
$date = new DateTime('now');
$date->modify('+2 days');
echo $date->format('Y-m-d H:i:s');
$date = new DateTime('now');
$date->modify('+4 hours');
echo $date->format('Y-m-d H:i:s');
You need to use the strtotime() function (http://php.net/manual/en/function.strtotime.php).
For your examples:
//+2 hours<br>
strtotime("+2 hours");
// +2 days<br>
strtotime("+2 days")
Edit: for what you ask, about posted values, the syntax is like this:
strtotime("+2".$_POST['field_name']." days");
You can use hours/days/months/weeks/years and either + or -
I receive a string representing a date in the format YmdHis. When I turn it into a DateTime object and print it back out I get the timestamp for "now". Why is this happening?
$time = '20140718121314';
$format = 'YmdHis';
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC'));
$dt->createFromFormat($format, $time)
$dt->format($format); // equals 20140731001832 (i.e. 'now')
I figured it out. It's an order of operations issue. The timezone has to be set after DateTime::createFromFormat().
createFromFormat is static method. Example of usage:
$dt = DateTime::createFromFormat($format, $time);
About static methods
In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
What is the best way to do this?
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime() this will account for daylight savings time and leap year.
$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.3
$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.4
echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// Available in PHP 5.5
$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
If you want to do this in PHP:
// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));
If you want to add the date in MySQL:
-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
The DateTime constructor takes a parameter string time. $time can be different things, it has to respect the datetime format.
There are some valid values as examples :
'now' (the default value)
2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02
Use strtotime to convert the string to a time stamp
Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
eg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
You can use as following.
$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));
You can also set days as constant and use like below.
if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)
$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));
Here '+2 days' to add any number of days.
One liner !
echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');