Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split Time into 3 Variables

Tags:

php

datetime

I have a time "7:00 am" that I need to split into $hour, $min, $ampm. Is there an easy way that I can split this in to 3 variables?

like image 258
Jason Small Avatar asked Jul 12 '26 21:07

Jason Small


2 Answers

$a      = preg_split('/[: ]/', '7:00 am');
$hour   = $a[0][0];
$minute = $a[1][0];
$ampm   = $a[2][0];
like image 93
serno Avatar answered Jul 15 '26 10:07

serno


$yourtime = "7:00 am";
preg_match("/([0-9]{1,2}):([0-9]{1,2}) ([a-zA-Z]+)/", $yourtime, $match);
$hour = $match[1];
$min = $match[2];
$ampm = $match[3];
like image 25
genesis Avatar answered Jul 15 '26 10:07

genesis