What's the simplest method to do this in PHP?
I need to make variables to be used in SQL queries.
For example, today is 06-MAY-2015.
$current_monday = '05-MAY-2014'
$current_friday = '09-MAY-2014'
$last_monday = '28-APR-2014'
$last_friday = '02-MAY-2014'
DateTime() accepts relative formats which makes this easy to do:
$current_monday = (new DateTime('Monday this week'))->format('d-M-Y');
$current_friday = (new DateTime('Friday this week'))->format('d-M-Y');
$last_monday = (new DateTime('Monday last week'))->format('d-M-Y');
$last_friday = (new DateTime('Friday last week'))->format('d-M-Y');
05-May-2014
09-May-2014
28-Apr-2014
02-May-2014
Demo
Not sure what your question was,
but perhaps you can try:
$time = time(); //today
$tmr = $time+(24*60*60);
echo date('Y-m-d', $tmr);
also:
http://www.php.net/manual/en/datetime.modify.php
see also strtotime() http://php.net/manual/en/function.strtotime.php
strtotime("+1 week");
$nextMonday = new DateTime('next monday');
$nextFriday = new DateTime('next friday');
$thisMonday = new DateTime('this monday');
$thisFriday = new DateTime('this friday');
$lastMonday = new DateTime('last monday');
$lastFriday = new DateTime('last friday');
Now to make this usable in mysql, just add ->format('Ymd')
... Eg. $thisMonday->format('Ymd');
This will return a date in the format of 20140506 which is friendly to DateTime columns in Mysql
Related
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);
Whats the best method to find the date in date('m.d.Y') format that is 1 day ago from another date in date('m.d.Y') format in PHP? Must be applicable to v5.1.6.
I need to build a recursive method to iterate through previous days.
Thanks!
------------Revision -------
$date = date('m.d.Y');
$date = date('m.d.Y', strtotime($date .' -1 day'));
The doesn't seem to work :(
The date must begin and end in the same format 'm.d.Y'
You really should be looking at using DateTime, DateInterval, and DatePeriod classes for this. As an example:
$start_date = '12.31.2013'; // your input date
$start_date_time = DateTime::createFromFormat('m.d.Y', $start_date);
$one_day_interval = new DateInterval('P1D');
// iterate X number of days example
$iteration_days = 30; // some value for number of iterations you want
$count_period = new DatePeriod($start_date_time, $one_day_interval, $iteration_days);
foreach($count_period as $day) {
// do something
}
// iterate between start and end date example
$end_date = '3.31.2014';
$end_date_time = DateTime::createFromFormat('m.d.Y', $end_date);
$date_period = new DatePeriod($start_date_time, $one_day_interval, $end_date_time);
foreach($date_period as $day) {
// do something
}
$date = DateTime::createFromFormat('m.d.Y', $yourDate);
$date->sub(new DateInterval('P1D');
try like this:
$date =date("Y-m-d"); //set your date
echo date('m.d.Y', strtotime($date .' -1 day'));
demo : https://eval.in/107144
WHAT I AM DOING
I want to find the difference between 2 datetime and add it to another datetime. I am only able to get the difference in Y-m-d H:i:s format.
CODE
$begin = new DateTime($start);
$finish = new DateTime($end);
$diff = $begin->diff($finish);
$difference = $diff->format("%Y-%M-%D %H:%I:%S");
Here I want to add $difference to another datetime say $finaldate. If its not possible is there any way of getting the difference in only minutes, then i could use $date->modify("+$difference minutes");
*This is a method using DateTime:*
$begin = new DateTime($start);
$finish = new DateTime($end);
$difference = $finish->format('U') - $begin->format('U');
// working version
$minutesDiff = round(($difference/60), 0);
$finalDate = new DateTime();
$finalDate->modify(sprintf('+%s minutes', $minutesDiff));
edit
added missing bracket
edit2
version without ->diff() method
What about:
$begin = strtotime($start);
$finish= strtotime($end);
$diff = $finish-$begin;
$finaldate = strtotime($finaldate)+$diff;
echo date("Y-M-D h-i-s",$finaldate);
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');
For some reason, I cannot get strtotime('+1 month) to work. Here is my code;
$Date = $_REQUEST['date']; //This is a unix time stamp
$Start = $_REQUEST['start']; //This is a unix time stamp
$End = $_REQUEST['end']; //This is a unix time stamp
to add a month onto my dates;
$monStart =strtotime('+1 month', $Start);
$monEnd =strtotime('+1 month', $End);
$monDate =strtotime('+1 month', $Date);
then to show my changed dates;
$vEnd = date('m/d/Y', $monEnd);
$vStart = date('m/d/Y', $monStart);
$vDate = date('m/d/Y', $monDate);
The problem that I have is that the supplied dates;
$Date = 1/31/2013
$Start = 1/01/2013
$End = 1/31/2013
Return;
$vDate = 3/03/2013
$vStart = 2/01/2013 //Only correct one
$vEnd = 3/03/2013
Please can someone help me?
It's jumping to March because today is 31sth Jan, and adding a month gives 31st Feb, which doesn't exist, so it's moving to the next valid date. This is a PHP bug. You can get more info on that at https://bugs.php.net/bug.php?id=44073
You can try with DateTime to over come this scenario. You can use this function for your requirement
function add_month($date_value, $months, $format = 'm/d/Y') {
$date = new DateTime($date_value);
$start_day = $date->format('j');
$date->modify("+{$months} month");
$end_day = $date->format('j');
if ($start_day != $end_day)
$date->modify('last day of last month');
return $date->format($format);
}
Now you can call :
$vEnd = add_month($monEnd, 1);
$vStart = add_month($monStart, 1);
$vDate = add_month($monDate, 1);
This will give you :
$vDate = '02/28/2013';
$vStart = '02/01/2013';
$vEnd = '02/28/2013';
Hope this helps you :)
DateTime is much better for handling date math as it account for things like days in the month:
$dt = new DateTime('2013-02-01');
$dt->modify('+1 month');
echo $dt->format('Y-m-d');
See it in action
Since you're using timestamps it might look like this:
$dt = new DateTime('#'.$_REQUEST['start']);
$dt->modify('+1 month');
echo $dt->format('m/d/Y');