I need to add days to given string date and display calculated date in string
This is what I have tried, but I could not make it work.
$date = date_create('1-Feb-2012');
$newDate = date_modify($date, '+2 day');
echo 'Your date is' . $newDate . '.';
This gives an error
Object of class DateTime could not be converted to string
You need to tell the DateTime object how to format its output using DateTime::format. So, for example:
$date = new DateTime('1-Feb-2012');
$date->modify('+2 day');
echo 'Your date is' . $newDate->format('Y-m-d H:i:s') . '.';
Also note that modify directly modifies the DateTime - it doesn't just return a new one, as the documentation might lead you to believe - so I've removed the second variable. I've taken the liberty of changing the objects to the object-oriented form as well, which you should be using :)
Here's a working demo.
Use DateTime::format function.
usage:
echo 'Your date is' . $newdate->format('Y-m-d H:i:s') . '.';
or
echo 'Your date is' . date_format($newdate, 'Y-m-d H:i:s') . '.';
Related
Code
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date']);
$order_dispatch_date = $order_dispatch_date->format('Ymd');
$order_collection = date('Ymd', strtotime('next thursday', $order_dispatch_date));
$order_collection = new DateTime($order_collection);
echo '['.$order_collection->format('d/m/Y') . ' - ' . $order_dispatch_date . ' - ' . gettype($order_dispatch_date) . ' ] ';
I get the following output if the input is: 20171128
[29/08/1970 - 20171128 - string ]
My question is why is the first output showing 29th August 1970 rather than 20th November 2017?
Stop changing things back-and-forth between objects, strings and timestamps so much, especially when you're re-using the same variables. It's needlessly hard to follow.
If you've got a Ymd formatted string, and you want an equivalent string back for the proceeding Thursday, just use this:
$inc['order_dispatch_date'] = '20171128';
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date'])->modify('next thursday');
echo $order_dispatch_date->format('Ymd');
// 20171130
See https://eval.in/909998
Basically:
$order_collection = date('Ymd', strtotime('next thursday', strtotime($order_dispatch_date)));
And that should work :)
I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');
How do you take an existing 'date and time' value and convert it to the same date but a specified time?
For example, $time="2017-09-01 13:18:00" -> how to do you convert to "2017-09-01 23:59:59"? It must keep the date but change the time to 23:59:59.
You can do it like this by explode and simple concatenation
<?php
$time="2017-09-01 13:18:00";
$date = explode(" ", $time)[0];
echo $date." 23:59:59";
?>
Live demo : https://eval.in/853822
Update
I think you need this
<?php
$date = new DateTime('2017-09-01 13:18:00');
$date->setTime(23, 59,59);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Live demo : https://eval.in/853857
What about this?
$date = new DateTime('2017-09-01 13:18:00');
$date->add(new DateInterval('PT10H30S'));
I have dates stored in a MySQL database like so: 2012-02-10
When i output them using PHP, is there a function I can use that will output it like so 10/02/2012
Ive tried:
$theDate = date_format($row['date'], 'd/m/Y');
echo $theDate;
but it doesnt seem to work. Any help appretiated.
PHP Version 5.3.3
You need to use date_create() before using date_format(). This is because date_format() expects a DateTime object as the first parameter.
$date = date_create($row['date']);
echo date_format($date, 'd/m/Y');
Another way to do the same thing:
$dt = new DateTime('2012-02-10');
echo $dt->format('d/m/Y');
For the PHP 5.4 users out there it can be simplified to:
echo (new DateTime('2012-02-10'))->format('d/m/Y');
edit
To comment on the alternative solutions provided, they can be simplified to:
echo date('d/m/Y', strtotime($row['date']));
Just keep in mind that they do not account for daylight savings time or timezones like DateTime does.
The old way (non-OOP) to do it,
$t = strtotime('2012-02-10');
echo date('d/m/Y', $t);
Functions to check: strtotime, date
Here's a really simple way to do it
<?php
$theDate = $row['date'];
echo date("m/d/Y", strtotime($theDate));
?>
Here are a few examples:
$input = '2012-02-10';
// datetime (object oriented style)
$dt = new DateTime($input);
echo $dt->format('d/m/Y') . "\n";
// datetime (procedural style)
$dt = date_create($input);
echo date_format($dt, 'd/m/Y') . "\n";
// strtotime
$m = strtotime($input);
echo date('d/m/Y', $m) . "\n";
// substr
echo substr($input,8,2) . "/" . substr($input,5,2) . "/" . substr($input,0,4) . "\n";
// explode
$m = explode('-', $input);
echo "$m[2]/$m[1]/$m[0]" . "\n";
// preg_match
preg_match('~^(\d+)-(\d+)-(\d+)$~', $input, $m);
echo "$m[3]/$m[2]/$m[1]" . "\n";
// sscanf
$m = sscanf($input, '%d-%d-%d');
echo "$m[2]/$m[1]/$m[0]" . "\n";
p.s. did I miss any? ;)
I'd be happy to help code that does not really work
I want to see if the project was published last month or not.
If yes then get a positive result.
//$project_time="2012-08-01 13:43:49";
$project_time="2012-10-02 14:05:09";
$end=mktime(0,0,0,date("m",strtotime($project_time))+1,date("d",strtotime($project_time)),d ate("y",strtotime($project_time)));
$end=date("d.m.y",$end);
$today=mktime(0,0,0,date("m"),date("d"),date("y"));
$today=date("d.m.y",$today);
echo 'Project date '.$date.'<br />';
echo 'End date '.$end.'<br />';
echo 'Today '.$today.'<br />';
if($today<$end){
echo " open<br />";
}
else{
echo " finish<br />";
}
PROJECT_TIME first gives a good result and the other not.
$ end create date based on $ PROJECT_TIME plus one month.
Variable data TOTDAY get today's date.
And the comparison I want to get an answer whether past month from PROJECTTIME
If someone has understood and can help I would be happy.
strtotime is the function you want to use. Simply use the following syntax:
$end = date('d.m.y', strtotime('+1 month', strtotime($project_time));
EDIT
What people are saying about comparing strings is correct. Don't compare strings, compare the timestamps.
Try rewriting it to something like:
$project_time = "2012-10-02 14:05:09";
$project_endtimestamp = strtotime('+1 month', strtotime($project_time));
echo 'Project date ' . $date . '<br />';
echo 'End date ' . date('d.m.y', $project_endtimestamp) . '<br />';
echo 'Today ' . date('d.m.y') . '<br />';
if (time() < $project_endtimestamp) {
echo " open<br />";
} else {
echo " finish<br />";
}
EDIT: Didn't completely understood the question. Added +1 month in strtotime call as per #Simon Germain answer.
Basic problem: You're comparing two "d.m.y" strings.
This will always fail, because PHP sees them as plain text, not as dates. Therefore, asking which one is bigger will generally give the wrong answer.
Also: Get rid of all that crazyness with the old-style date handling functions. PHP has much better ways to do that sort of thing these days.
$project_time="2012-10-02 14:05:09";
$projDate = DateTime::createFromFormat('Y-m-d H:i:s', $project_time);
$dateNow = new DateTime();
if($projDate < $dateNow) {
... do something here...
}