Merge Date and Time into one variable in php - php

I have got 2 variables like:
$date //2011-01-01
$full_old_date //1999-01-01 12:00:00
Now I want to combine the new date and the time of the $full_old_date together as a new variable like:
$full_new_date //2011-01-01 12:00:00
What should I do to extract only the time from the old one and combine them together as a new date variable?

Hope this helps (assuming you have these values as 'strings' and not date/timestamps):
$date = "2011-01-01";
$full_old_date = "1999-01-01 12:00:00"
$full_new_date = $date . ' ' . trim(substr($full_old_date,-8));
//substr() - This gets the last 8 characters of the $full_old_date variables
//trim() - This function removes any whitespace at the edges of the substring
There are many ways to do this, however this is one way of doing it.

This solution may help you
$date ="2011-01-01";
$full_old_date ="1999-01-01 12:00:00";
function newDate($date,$full)
{
return $date . " " . explode(" ",$full)[1];
}
echo newDate($date,$full_old_date);

You can use preg_replace like this
$date = "2011-01-01";
$full_old_date = "1999-01-01 12:00:00";
echo $new_date = preg_replace('/\d{4}-\d{1,2}-\d{1,2}/', $date, $full_old_date);

Related

PHP: Merge two date objects into one

I've got two date objects sent from a date input and a time input.
I want to generate a DateTime object with the yyyy-mm-dd from the date input and hh-mm from the time input.
Data sent from date input
[search_from_date] => 2015-08-04T23:00:00.000Z
Data sent from time input
[search_from_time] => 1970-01-01T02:02:00.000Z
I need to merge these two dates into:
2015-08-04 02:02
I've been playing around with exploding the string, etc to no avail,
Any help would be appriciated
Cheers
You can create two DateTime objects from your strings, then use the second to set the time on the first.
$arr = ['search_from_date' => '2015-08-04T23:00:00.000Z', 'search_from_time' => '1970-01-01T02:02:00.000Z'];
$date = new DateTime($arr['search_from_date']);
$time = new DateTime($arr['search_from_time']);
$date->setTime($time->format('H'), $time->format('i'), $time->format('s'));
echo $date->format('r');
Here's an eval.in with an example - https://eval.in/424209
you can split on "T" like that :
$search_from_date = "2015-08-04T23:00:00.000Z";
$search_from_time = "1970-01-01T02:02:00.000Z";
$tab_date = explode("T", $search_from_date);
$tab_time = explode("T", $search_from_time);
$date_time = new DateTime("$tab_date[0]T$tab_time[1]");
var_dump($date_time);
Try this,
$search_from_date = '2015-08-04T23:00:00.000Z';
$search_from_time = '1970-01-01T02:02:00.000Z';
$data = explode('T',$search_from_date);
$data1 = explode('T',$search_from_time);
$data1 = explode('.',$data1[1]);
echo $data[0].' '.$data1[0];
$time=new DateTime('1970-01-01T02:02:00.000Z');
$date=new DateTime('2015-08-04T23:00:00.000Z');
$newDateTime= new DateTime();
$newDateTime->setTime($time->format('H'),$time->format('i'),$time->format('s'));
$newDateTime->setDate($date->format('Y'),$date->format('m'),$date->format('d'));
echo $newDateTime->format('Y/m/d H:i'));
exit;
Something like this should work.
$finalDateD creates a date string for just the Year, month and day
and
$finalDateT creates a date string for just the Hours, minutes
This is then concatenated into the variable $finalDate;
$finalDateD = date('Y-m-d', strtotime($search_from_date));
$finalDateT = date('H:i', strtotime($search_from_time));
$finalDate = $finalDateD.' '.$finalDateT;

STRTOTIME take date and data from mysql and concatenate them on php

I have 2 variables stored into mysql:
campaign_date Format: d/m/Y
campaign_time Format: 24Hr
How could I concatenate them into one single variable like this:
2015-06-16T18:30
I tried with:
$new_datetime=$campaign_date.'T'.$campaign_time;
But it's not working
This should work for you:
(For the first date you have to change / to - so you can use date() and you can change the order of d/m/Y to Y-m-d, after that it's a simple concatenation with the time at the end)
<?php
$campaign_date = "16/12/2014"; //Data from DB
$campaign_time = "18:00"; //Data from DB
echo $new_datetime = date("Y-m-d", strtotime(str_replace("/", "-", $campaign_date))) . "T" . date("H:i", strtotime($campaign_time));
?>
Output:
2014-12-16T18:00
Try it,i tested it myself.
$db_date = date("Y-m-d",strtotime($db_date));
$db_time = date("h:i:s",strtotime($db_time));
echo $db_date.'T'.$db_time;

Add 1 year to a date

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

PHP replace a day number with a variable

I have this code:
$x = date("Y-m-d", strtotime($post['commissionEligibilityDate'] . "+ " . $post['billingPeriodExpiration'] . " months"))
$post['commissionEligibilityDate'] = 2011-11-08 <br/>
$post['billingPeriodExpiration'] = 2 <br/>
so $x returns 2012-01-08.
I have another variable $singleDate and it's equal to 1. What I am trying to do replace the 08 with 01. How can I do that?
You don't have to use Y-m-d, you can use Y-m-01 or your variable:
$x = date("Y-m-".$singleDate, strtotime($post['commissionEligibilityDate'] . "+ " . $post['billingPeriodExpiration'] . " months"))
You could use the DateTime class:
$d = new DateTime($x);
$year = $d->format('Y');
$month = $d->format('m');
$d->setDate($year, $month, '01');
echo $d->format('Y-m-d');
str_replace('08','01',$post['commissionEligibilityDate']);
$explode = explode("-",$post['commissionEligibilityDate']);
$explode[2] = $singleDate;
$post['commissionEligibilityDate'] = implode("-",$explode);
$post['commissionEligibilityDate'] will now echo 2011-11-1
Almost anything is possible with PHP. My suggestion seeing as your looking to only get one number from a date that you consider checking out http://php.net/manual/en/function.date.php to see the various ways of handling the date() output. As you could easily output a day then add to it $z = date('d', time())+1; for example.
I am not sure what your doing with your dates specifically but to me it sounds like you might have a misconception of what they are, and how to work with them. Basically the short idea of it is, a date defined in a variable is a string. You can make them anyway you want even without the use of date() then store them, as long as they are in the right format when you go to store them ie yyyy-mm-dd you should be fine.

change dd/mm/yy date format to yy/mm/dd using php

I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');

Categories