How i can change current date not all just days? - php

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

Related

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

strtotime with variable using php - object given

This is my current code:
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year");
$arrivalTime = date('Y-M-d H:i:s', strtotime("+$flightTimeMin minutes", $dateGame));
This isn't working because I believe "$dateGame" is an object. How would I turn it into something readable by "strtotime"?
Thanks
You dont need to use strtotime() since you are using datetime and you can use datetime object to format the date as
$universeTime = 3 ;
$flightTimeMin = 20;
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year +$flightTimeMin minutes");
If you want to format the display you can use as
echo $dateGame->format('Y-m-d H:i:s');
or just use
echo $dateGame->date ;

Quicker way to get date of ISO week from a date string?

I'm being passed a date string (most likely in ISO8601 format) and need to convert it to the date of the ISO week to store as a DATETIME column in MySQL. To initialize the DateTime object the I want to save, I'm doing the following:
$date = new DateTime("now");
$date = new DateTime( $date->format("o-\WW") );
echo $date->format(DateTime::ISO8601) . "\n";
Since I'm using Doctrine2, I need to pass my entity a DateTime object. Is there a way to avoid making 2 DateTime objects to get the same result? Should I drop back to the date function and use that as the argument to the DateTime constructor?
$date = new DateTime( date("o-\WW", strtotime("now") );
You could use setISODate to update the first DateTime object using the week and year of the object via format():
$date = new DateTime();
$date->setISODate($date->format('o') , $date->format('W'));
echo $date->format(DateTime::ISO8601);
You could use modify() method of DateTime object.
$date = new DateTime();
$date->modify('sunday this week');
echo $date->format(DateTime::ISO8601) . "\n";
Note that if you want the first day of the week to be something other than Sunday, you will likely need to do something like the following. This example considers Monday as the first day of the week, thus for dates on a Sunday, you would need to get the date of the Monday from the previous week.
$date = new DateTime();
if ($date->format('D') === 'Sun') {
$date->modify('monday last week');
} else {
$date->modify('monday this week');
}
echo $date->format(DateTime::ISO8601) . "\n";
You can probably use date like this:
$date = new DateTime( date('o-\WW') );
Though that formatting looks a bit strange. :p You can of course also use some other method/function the class has to offer to change/modify the date.

How to find date after certain period in 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');

Categories