I have a php time "09:00" that I want to set as the time for a date.
$date="2016-08-21 00:00:00.000000";
$time="09:00";
So ideally:
$datetime=$date+$time;//<----------what is the function for this
echo $datetime;
gives "2016-08-21 09:00:00.000000"
or
$date="2016-08-21 00:00:00.000000";
$time="17:30";
then
$datetime=$date+$time;//<----------what is the function for this
echo $datetime;
gives "2016-08-21 17:30:00.000000".
A seemingly simple requirement I cannot find a solution to.
try this :
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Pending Mark adding an answer this was what I had but it seems clumsy - I hoped there was a better way:
$timebits = explode(":", $timepart);
$now->setTime($timebits[0], $timebits[1], $timebits[2]);
Related
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'));
we want to make a script that renames the file name with a date stamp, we're new to PHP so this is what we have now.
<?php rename("test.txt", "tes3.txt"); ?>
we tried some with things like this below but we don't get it to work.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
I hope you guys have a answer how we can do this.
Thanks
All you have to do is use the textual output (string) produced by $date->format(...) and inject the answer in the rename call.
$date = new DateTime();
rename("test.txt", "test" . $date->format('Y-m-d H:i:sP') . ".txt");
But it is advisable to not use any spaces in filenames, especially if you want the files to be accessible through a server/website/...
In my project i have to get the expiry date of a package from the 'date of purchasing' the package and the 'validity in days' of the package.I got a code from the php.net as follows
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
Now suppose the date of purchase is like
2013-01-17
and the validity in days is
180
Now if i use the php.net example as follows
<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>
Will i get the output as 2013-04-17?
I need the output like
yyyy-mm-dd
I cant run the program in my personal computer but i have to carry on the codding of the project so i'm here.
<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>
//output
//2013-07-16 and it is in yyyy-mm-dd format
working example http://codepad.viper-7.com/gAtT2D
and if you can't run the program in your PC, use online debugger shell.
I want to get today's date + one year. How do I achieve this with PHP's date functions?
echo date('Y', strtotime('+1 year'));
You can use strtotime and date
$date = '2010-09-16';
echo date('Y-m-d', strtotime("+12 months $date"));
// 2011-09-16
On a sidenote: DateTime questions like this have been answered over and over again, so you could have found how to add to a date easily by using the search function.
From PHP's documentation:
<?php
$date = new DateTime($your_supposed_date);
$date->add(new DateInterval('P1Y'));
echo $date->format('Y-m-d') . "\n";
?>
Gordon's much cleaner version (Thank you!):
<?php
$date = new DateTime("+12 months $theDate");
echo $date->format('Y-m-d') . "\n";
?>
$Ad_year = 2015-10-20
<?php echo $Ad_year + 1?>
Result 2016
The shortest version:
echo (int)date('Y') + 1;
You could use the new Datetime and Datetime_Intervall-classes introduced in the later PHP 5-versions.
I once posted an answer in this question. Maybe it helps you :)
The advantage is, that this classes also checks for leap-seconds and leap-years, timezones, etc.
If you're working with timestamps
echo time()+60*60*24*365
Best and easy solution...
You can change month or year or day.
date('Y-m-d',strtotime("+1 day +2months +1 year"));
Below code also return next year from current date:
<?php echo date('Y', strtotime('+12 month'));>