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'));
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');
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]);
I'm upload the excel data into mysql. in there date save like 16.5.59(dd.mm.yy) formate. I try to change this using this code
$date = '16.5.59';
echo date('d-m-y',strtotime($date));
it always show current time like 04-02-15.
Pls change this to dd-mm-yyyy formate.
Thanks in Advance.
Timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
If it not cover the between given range then it will take current date.
$date = '16.5.59';
$date_array = explode(".",$date);
$var_day = $date_array[0];
$var_month = $date_array[1];
> Blockquote
$var_year = $date_array[2];
echo $new_date_format = "$var_day-$var_month-$var_year";
Try to make the date yourself using explode and strtotime:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_time = strtotime(implode('-', array_reverse(explode('.', $date))))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
Output: 16-5-2059
You ARE required to specify that the year is 1959 instead of 2059 (or are you sure that it's really 2059?)
You can make change from 2059 to 1959 like this:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_arr = array_reverse(explode('.', $date))
# This line does the job. Make sure all years are between 1900 - 1999.
$date_arr[0] = '19' . $date_arr[0];
$date_time = strtotime(implode('-', $date_arr))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
I hope this answer can help you.
This is by no means the best solution but using php's date_parse_from_format is possible. You could supply a format and use the following
$date = "16.5.59";
$dateObj = date_parse_from_format("j.n.yy", $date)
with the $dateObj you can work what you need such as :
echo $dateObj['year'];
IDE Running Example
Worth noting you require PHP >=v5.3
You can do this if it is for simpler task. It will consume time.
$date = '16.5.59';
$dtSplit=explode(".",$date);
echo "<br>".$dtSplit[0].".".$dtSplit[1].".".$dtSplit[2];
echo "<br>".$dtSplit[0].".".$dtSplit[2].".".$dtSplit[1];
echo "<br>".$dtSplit[1].".".$dtSplit[0].".".$dtSplit[2];
echo "<br>".$dtSplit[1].".".$dtSplit[2].".".$dtSplit[0];
echo "<br>".$dtSplit[2].".".$dtSplit[0].".".$dtSplit[1];
echo "<br>".$dtSplit[2].".".$dtSplit[1].".".$dtSplit[0];
I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015
I have an events feed that needs the dates in this format.
query.setMinimumStartTime("2011-12-15");
query.setMaximumStartTime("2011-12-19");
When I pull from the database <?php $startmin = $line['StartDate'];?>
it comes in this format "12/15/2011"
I use <?php $fstartmax = str_replace('/','-',$startmax);?>
to change the "/" to "-" and now have 12-15-2011
how can I make it 2011-12-15??
$date = new DateTime('12/15/2011');
$interval = new DateInterval('P1D');
$date->add($interval);
echo $date->format('Y-m-d');
See http://fr2.php.net/manual/en/class.datetime.php & http://fr2.php.net/manual/en/class.dateinterval.php for more info.
You can use strtotime() to get a timestamp and then date with the resulting timestamp, like so:
echo date("Y-m-d", strtotime("12/15/2011"));