How to split date and time from a string like 8/29/2011 1:00 PM in laravel - laravel

in my blade there is a DateTime picker which selects in mm/dd/yyyy hh:mm AM/PM format. and i need to extract this to date = 8/29/2011 and time = 13:00 .
how can I do that?

Do as below.
$stringdate = "8/29/2011 1:00 PM";
$timestemp = strtotime($stringdate);
$date = date('Y-m-d', $timestemp);
$time = date('H:i:s',$timestemp);
echo $date;
echo $time;
check example
Edit:- as you want this format YYYY-MM-DD hh:mm
$datetime = date('Y-m-d H:i', strtotime("8/29/2011 1:00 PM"));
echo $datetime; //output "2011-08-29 13:00";

Try this way.
#php
$input = '2017-06-27 12:58:20';
$format1 = 'Y-m-d';
$format2 = 'H:i:s';
$date = Carbon\Carbon::parse($input)->format($format1);
$time = Carbon\Carbon::parse($input)->format($format2);
#endphp

It is simple just try it
$date="8/29/2011 1:00 PM"; // date that get from view (blade page)
$createTimestamp = new DateTime($date); // create timestamp object from string
$date = $createTimestamp->format('m/d/Y'); // get date
$time = $createTimestamp->format('H:i:s A'); // get time with AM/PM format
return "date: ".$date." time: ".$time; // return your result

Try this way
Use Carbon;
$dateAndTime=Carbon::parse('8/29/2011 1:00 PM');
$date=$dateAndTime->format('d-m-Y');// 29-8-2011
$time=$dateAndTime->format('H:i:s');// 1:00
$time=$dateAndTime->format('H:i:s A');// 1:00 AM/PM
Refer this link to understand more about Carbon.

Related

How to calculate experience by using date with current date in laravel?

I am using laravel framework to develop api's , i have one column inside table with timestamp,i am fetching that value and i want to show that value to the 3 Years 2 Month or if it's been in days it should show 10days, i have tried by using carbon package to convert as per my requirement but i can't able to figure out can you please help me to achieve this one.
Ex1:-
$date = 2022-09-15 00:00:00;
//my expectation is **8days**
Ex2:-
$date = 2021-08-23 00:00:00;
//my expectation is 1 year 1 month
Here is the example you can do like this
$Born = Carbon\Carbon::create(1986, 1,3);
$Age = $Born->diff(Carbon\Carbon::now())->format('%y Year, %m Months and
%d Days');
echo $Age;
Here is your date example is working and it's result
$date1 = \Carbon\Carbon::create("2022-09-15 00:00:00");
$date2 = \Carbon\Carbon::create("2021-08-23 00:00:00");
$totalYearMonthDate = $date1->diff($date2)->format('%y Year,
%m Months and %d Days');
Result:-
1 Year, 0 Months and 23 Days
diffForHumans has parts and minimumUnit options that do what you want:
$options = [
'parts' => 2,
'minimumUnit' => 'day',
'skip' => ['week'],
];
echo Carbon::parse('2022-09-15 00:00:00')->diffForHumans($options) . "\n";
echo Carbon::parse('2021-08-23 00:00:00')->diffForHumans($options) . "\n";
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference ". $interval->y . " years, " .
$interval->m." months, ".$interval->d." days ";
Try doing like this

How to get 1st, 2nd, 3rd week format carbon laravel

I need to get 1st, 2nd, 3rd and 4th week format from CarbonPeriod. What should i do to make it in text?
$carbonPeriod = CarbonPeriod::create($startDate, '1 week', $endDate);
foreach ($carbonPeriod as $period) {
$startPeriod = clone $period;
$endPeriod = clone $period;
$startRange = $parsedStartDate->gt($startPeriod->startOfWeek()) ? $startDate : $startPeriod->startOfWeek()->format('Y-m-d');
$endRange = $parsedEndDate->lt($endPeriod->endOfWeek()) ? $endDate : $endPeriod->endOfWeek()->format('Y-m-d');
$labels[] = $period->translatedFormat('F D Y'); //i need Jan 1st week 2021, Jan 2nd week 2021 and etc for the labels
}
$carbonPeriod = CarbonPeriod::create($startDate, '1 week', $endDate);
foreach ($carbonPeriod as $index => $period) {
$startPeriod = clone $period;
$endPeriod = clone $period;
$startRange = $parsedStartDate->gt($startPeriod->startOfWeek()) ? $startDate : $startPeriod->startOfWeek()->format('Y-m-d');
$endRange = $parsedEndDate->lt($endPeriod->endOfWeek()) ? $endDate : $endPeriod->endOfWeek()->format('Y-m-d');
$labels[] = $carbonPeriod->translate('ordinal', [':number' => $index + 1]);
}
If I understand you correctly you want the ordinal week within the given period. If that's indeed the case then you can do what you need using:
$locale = 'xxx'; // Replace with your locale here
$nf = new NumberFormatter($locale, NumberFormatter::ORDINAL);
$carbonPeriod = CarbonPeriod::create($startDate, '1 week', $endDate);
foreach ($carbonPeriod as $i => $period) {
$startPeriod = clone $period;
$endPeriod = clone $period;
$startRange = $parsedStartDate->gt($startPeriod->startOfWeek()) ? $startDate : $startPeriod->startOfWeek()->format('Y-m-d');
$endRange = $parsedEndDate->lt($endPeriod->endOfWeek()) ? $endDate : $endPeriod->endOfWeek()->format('Y-m-d');
$ordWeek = $nf->format($i);
$labels[] = $period->translatedFormat('F')." $ordWeek". __("week")." ".$period->translatedFormat('Y'); // Should say "January 1st week 2021
}
Note this requires the intl extension
$CarbonPeriod = Carbon::now()->subDays(20);
$today = Carbon::now();
$weeks = intval(floor(($CarbonPeriod->diff($today)->days) / 7)) . 'st';
Result "2st"

How to use timezones in Laravel?

The user creates date in format like as: 12/23/2016 22:10
After this data should be sent convert to another timezone, for example GMT + 2:
12/23/2016 00:10
I can assume the date that I create should be saved in GTM 0, only after I can change timezone.
How can I use this mechanism in Laravel?
You can use Carbon to change the the timezone of a date as:
$date = Carbon\Carbon::parse('12/23/2016 22:10', 'GMT');
And to add +2 you can do as:
$date->addHours(2)
You can change the timezone for your application in: /config/app.php,
The default timezone is UTC. You can find all supported timezones here: http://php.net/manual/en/timezones.php
Mostly I'm agree with Amit Gupa answer, but the best way to manage timezone conversion isn't by adding hours, but by converting it on Carbon:
$yourdate->timezone('somecountrytimezone')
This way you won't have to worry about different time management between countries (Like daylight saving time) and just manage to get your works done.
Please refer do the documentation : http://carbon.nesbot.com/docs/#api-settersfluent
We can use parse and createFromFormat methods of Carbon to set our datetime as a certain timezone then use setTimezone to convert it to another timezone.
$date = "2022-08-05 12:00:00"; //set datetime for checking
$now = Carbon::now()->timezone('Europe/London')->format('Y-m-d H:i:s');
$createFrom = Carbon::createFromFormat('Y-m-d H:i:s', $date, 'Asia/Hong_Kong')->format('Y-m-d H:i:s');
$parse = Carbon::parse($date,'Asia/Hong_Kong')->setTimezone('Europe/London')->format('Y-m-d H:i:s');
$converted = Carbon::parse($date,'Asia/Hong_Kong')->timezone('Europe/London')->format('Y-m-d H:i:s');
outputs: when dd($now,$createFrom,$parse,$converted);
^ "2022-08-05 08:33:48"
^ "2022-08-05 12:00:00"
^ "2022-08-05 05:00:00"
^ "2022-08-05 05:00:00"
that's not related to laravel , it's just a php , so you can simply get the time-zone offset (exp GMT+3) then do like this :
$timeZoneOffset ---> for GMT+3 you put (3)
function getNewDateByTimeZone($date, $timeZoneOffset)
{
/* if the client Time zone is GMT */
if (!$timeZoneOffset || $timeZoneOffset == 0) {
$timeZoneOffset = "-0";
}
$op = $timeZoneOffset[0];
$timeZoneOffset = preg_replace('/[^0-9.]+/', '', $timeZoneOffset) * 60;
$userDate = date($date, time());
$timeStamp = strtotime($userDate);
$offsetTime = $op == '-' ? $timeStamp + $timeZoneOffset : $timeStamp - $timeZoneOffset;
return date("Y-m-d H:i", $offsetTime);
}
so like that you will save the date in GMT-0

Retrieving date format with am/pm in codeigniter

I have converted date to my local time as below:
$this->date_string = "%Y/%m/%d %h:%i:%s";
$timestamp = now();
$timezone = 'UP45';
$daylight_saving = TRUE;
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$this->updated_date = mdate($this->date_string,$time);
And I'm storing this field in to database.
Now at retrieval time I want format like this:
"11-04-2011 4:50:00 PM"
I have used this code:
$timestamp = strtotime($rs->updated_date);
$date1 = "%d-%m-%Y %h:%i:%s %a";
$updat1 = date($date1,$timestamp);
But this will give me only
"11-04-2011 4:50:00 AM"
But I have stored it like it was PM.
Might get voted down, but will have a go at it.
Is it because the MySQL stores it in 24 hour format? (assuming you are using the datetime field type)
Maybe this will help
Converting mysql TIME from 24 HR to AM/PM format
sorry if it doesn't.

Joomla - get one year and one month time()

I can get the current date&time by using
$today = JHTML::_('date', time(), '%Y-%m-%d %H:%M:%S');
echo $today;
How to I get 1year later and 1month later base on $today??
eg.now:2011-12-10 13:00:01
How to get 2012-12-10 13:00:01 AND 2012-01-10 13:00:01
It might not be the best way to do it, but this should work:
$time = mktime( date("H"), date("i"), date("s"), date("m")+1, date("d"), date("Y") + 1);
JHTML::_( 'date', $time, '%Y-%m-%d %H:%i:%s');
Alternatively, if you are using PHP 5.3, you can use the DateTime object.
I hope it helped!

Resources