How to find date after certain period in php - php

I am working in yii framework. i am getting current date in php (Yii framework) by-
$date =new CDbExpression('NOW()');
Its giving date in format-"2013-04-27 12:49:27".
I want to find date after one year. So how to find date in this format after one year or certain period in php?

Try this -
echo date('Y-m-d H:i:s', strtotime("+365 days"));

Have a look at DateTime class to manipulate with dates in php.
$newDate = new DateTime($date);
$newDate->modify('+1 year');
echo $newDate->format('Y-m-d H:i:s');
DEMO.

You can use alternate which look like this:
$date =new CDbExpression('NOW()');
$NextYear = date('Y-m-d H:i:s',strtotime($date)) . " + 365 day"));
If you want more specific you can try:
$date =new CDbExpression('NOW()');
$NextYearDate=date('Y-m-d',strtotime('+1 year',$date));

Or try this
$date = new CDbExpression('NOW() + INSTANCE 1 YEAR');

Related

How i can change current date not all just days?

I want change the current date or i want put variable in current date like this:
$companyDates = $company_dates['dates']; //this variable come from DataBase
$databaseDate=DateTime::createFromFormat("Y-m-d", $companyDates);
$day=$databaseDate->format('d'); // this is how i take just days from date format(y-m-d)
$dt = new DateTime();
$today = $dt->format('Y-m-d');
$oldDate=$dt->format('Y-m-$day'); // here i want days from database into current day month and year.
and then i want to find different day like this:
$date1=date_create($today);
$date2=date_create($oldDate);
$diff=date_diff($date1,$date2);
Is this possible OR is this my way right ?
Try this:Use setDate
$day=10;
$dt = new DateTime();
$today = $dt->format('Y-m-d');
$out = new DateTime();
$out->setDate($out->format('Y'), $out->format('m'), $day);
$oldDate= $out->format('Y-m-d');
$d1=new DateTime($oldDate);$d2=new DateTime($today);
$difference = $d1->diff($d2);
echo $difference->format('%r%a days');
You're trying to pass desired day inside format
$oldDate=$dt->format('Y-m-$day');
It's wrong format (and if you want to concatenate string with variable you should use double quote(") instead of single('))
Use setDate() method instead of.
$today = new DateTime();
$oldDate = clone $today; // Clone instead creating new instance because two different DateTime instances may have different dates
$oldDate->setDate($oldDate->format('Y'), $oldDate->format('m'), $day);

Setting Php and Mysql timestamp

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 -

build a new mysql variable from an existing date field

I have a date column in my MySQL table and I want to build a new variable from that whereby I will add a few days.
example:
$date = 2014-12-12
now I need a second variable $date2 whereby the date will be 2014-12-17
So I need something like this
$date2 = $date + 5 days
I've searched for this and I got solutions for building queries, but I want to have a second variable. Is this possible?
I tried this (with no luck)
$date2 = DateTime::createFromFormat('d-m-Y', $date1);
$date2->modify('5 day');
echo $date2->format('Y-m-d');
This should work:
$date2 = strtotime($date) + (60*60*24*5); //convert date to unix time stamp and add 5 days
$date2 = date('Y-m-d', $date2); //convert back to readable format
Or an even better approach:
$date2 = date('Y-m-d', strtotime($date . "+5 days"));
You are missing the + in your modify call:
$date2 = DateTime::createFromFormat('d-m-Y', $date1);
$date2->modify('+5 day');
echo $date2->format('d-m-Y');

how to convert int into time period

I have today date and i want to reduce hours\days from it. i get the "hours to reduce interval" in int that indicate number of days.
I tried something like this:
$today_date = date('Y-m-d H:i:s');
$temp_interval_date = $settings->days_back;
$interval_date = date('H',$temp_interval_date*24);
$final = $temp_interval_date - $interval_date;
My final goal is to get todaydate - interval period in this format
'Y-m-d H:i:s'
I am c# dude :)
Thanks
I'm not entirely clear on what you're asking but I think this is what you're looking for.
$date = new DateTime();
$date->sub(new DatePeriod('P'.$settings->days_back.'D'));
echo $date->format('Y-m-d H:i:s');
You can also do (if you're using PHP 5.2)
$date = new DateTime();
$date->modify('-' . $settings->days_back . ' days'));
echo $date->format('Y-m-d H:i:s');
reference
DateTime()
DatePeriod()
maybe this could be helpful:
echo date('Y-m-d', strtotime('-1 day', date('Y-m-d') ));
Another way to do it
<?php
$temp_interval_date = 2; //in hours (for example =2)
echo date('Y-m-d H:i:s', strtotime('-'.($temp_interval_date*24).' hours',strtotime(date('Y-m-d H:i:s'))));
?>

adding 1 day to a DATETIME format value

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

Categories