I need to isolate the time from a date / timestamp - php

I have a date string, or datestamp, whichever is the easiest.
The string looks like this:
Thursday, 01 December 2016 19:00 - 22:00
Or, if there is no end time:
Saturday, 01 December 2016 19:00
I can also get them as datestamps.
The $event object contains information like this:
[dtstart] => 1479232800
[dtstart] => 1481094000
[dtend] => 1481151599
//apparently if there is no end time, the endtime is set to 23:59:59.
I tried doing this:
echo 'start '.date('H:i','$event->dtstart')
But that didn't give me anything.

Change this line:
echo 'start '.date('H:i','$event->dtstart');
to
echo 'start '.date('H:i',$event->dtstart); // remove the quotes from 2 parameter and try again, it will give you the result like:
start 10:00

Remove quotes from $event->dtstart
echo 'start '.date('H:i',$event->dtstart);
example: echo date('H:i',1171502725);

Related

Changing time format from 9:00am to 09:00am in php

Iam having variable with value 9:30 am - 12:00 pm i.e
$val=$shift (o/p 9:30 am - 12:00pm)
I want to convert it to 09:30 am - 12:00pm, i mean i need to add 0 infront of number if time having one digit
If you are using date function then you can format time using h.
For example
echo date('h:ia'); // output 09:50am
See Referance
If you have values from DataBase as you mentioned in comment then you need to apply PHP's built-in function strtotime() before formatting the time.
For Example
echo date( 'h:ia', strtotime('9:30 am') ); // output 09:50am
Assuming you are saving your time in your database as a string as 9:30 am - 12:00pm you can just split them. Then format each data.
$val= "9:30 am - 12:00pm";
$splittedString = explode('-', $val);
$time1 = date('h:ia',strtotime($splittedString[0]));
$time2 = date('h:ia',strtotime($splittedString[1]));
echo $time1." - ".$time2;
Try
echo date('h:ia',strtotime("9:30 am"))." - ".date('h:ia',strtotime("12:00pm"));

Phpspreadsheet - Time cell retrieved as float

I have an excel file which has a time input.
Mar 01, 2018 | Thursday | 8:00 AM | 5:00 PM
Mar 02, 2018 | Friday | 8:00 AM | 5:00 PM
But when my code tries to read those cells, the output becomes a float (for example 8:00 AM becomes 0.33333333333333). This is my code
$date = $sheet->getCell("A".$row)->getValue();
$time_in = $sheet->getCell("C".$row)->getValue();
$time_out = $sheet->getCell("D".$row)->getValue();
echo "date: ".$date. //Mar 01, 2018
" time_in: ".$time_in. //becomes 0.333333333333333
" time_out: ".$time_out; //becomes 0.708333333333333
How can I make the output as is without the phpspreadsheet changing the value? I have tried looking at the phpspreadsheet documentation but I haven't found a solution.
Thankfully I have found the answer just now.
$in = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($time_in);
echo gmdate("g:i a", $in);
Hopefully, this could be useful for others.
Formart the data before you read it.
For example:
cell data 2020/10/13 19:00:00
Access directly will get 44117.791666667
$sheet->toArray();
Format before access will get 2020-10-13 19:00:00
$row_num = $sheet->getHighestDataRow();
$sheet->getStyle("H1:H$row_num")->applyFromArray(array("numberFormat"=>array("formatCode"=>'yyyy-mm-dd hh:mm:ss'))); // H is the datatime row
$sheet->toArray();

Adding and removing from a string containing dates using PHP

I have to edit a whole bunch of date intervals. But they are all mixed up. Most are in the form Month YearMonth Year
eg January 2014March 2015
How would I insert a hyphen in between so I end up with
January 2014 - March 2015
I also have the problem where these dates occur in the same year.
eg April 2012September2012
In such a case I would need to insert the hyphen and remove the year so that I'm left with
April - September
There must be some PHP string operators for stuff like this. Well thats what I'm hoping.
Would appreciate some guidance. Thanks in advance.
Thanks, sorry for my delayed reply
$string = "January 2014March 2015";
preg_match('/([a-z]+) *(\d+) *([a-z]+) *(\d+)/i', $string, $match);
print "$match[1] $match[2] - $match[3] $match[4]";
outputs,
January 2014 - March 2015
You could do it using lookaround:
$string = "January 2014March 2015";
$res = preg_replace('/(?<=\d)(?=[A-Z])/', ' - ', $string);
echo $res,"\n";
Output:
January 2014 - March 2015

strtotime and then convert to UNIX?

EDIT
Per the comment below, here's my attempt at splitting / combining / converting the string prior to implementing mktime...
$date1 = explode("T",'2005-03-27T00:00:00');
$date2 = explode("-", $date1[0]);
$try = mktime($time1,$time2,0,$date2[1],$date[2],$date[3]);
print $try;
// Prints out: 951793200
Original Question:
I've inherited a database and would like very much to convert the bizarre way the data is stored to something more mysql-friendly...
In the meantime, I get a text string (yes... text)... That I'd like to convert to unixtime...
So, I'll get a string that looks like this:
2005-03-27T00:00:00 03:00 AM
I'd like to convert it to:
1111885200
Dates and times always mess me up... I've done a number of things using strtotime and mktime, but can't get it formatted the way I want.
Well, this is how I would do it.
$ex = '2005-03-27T00:00:00 03:00 AM';
$format = '%Y-%m-%dT00:00:00 %H:%M %p';
$strf = strptime($ex, $format);
$date_str = $strf['tm_mon'] + 1 . '/' . $strf['tm_mday'] . '/' . ($strf['tm_year'] + 1900) . ' ' . $strf['tm_hour'] . ':' . $strf['tm_min'] . ':00';
echo $date_str;
echo "\n";
echo strtotime($date_str);
echo "\n";
echo date('m-d-Y H:i:s', 1111885200);
But, your desired result does not seem to be correct based on the date you posted.
OUTPUT
3/27/2005 3:0:00
1111921200
03-26-2005 17:00:00
You could write a litte converter script that does the task for you.
Please check the optional parameters of mktime
You could try to split your string in hour / minute / second / month / day / year and put that into mktime. Then mktime will give you the unix timestamp for that.
So, I'm not a fan of regular expressions unless absolutely necessary, but they might be necessary here to pick out the time zone piece.
preg_match("/(\d{4})-(\d{2})-(\d{2})(T\d{2}:\d{2}:\d{2}) (\d{2}:\d{2}) (AM|PM)/", '2005-03-27T00:00:00 03:00 AM',$matches);
Will give you $matches that look something like:
Array
(
[0] => 2005-03-27T00:00:00 03:00 AM
[1] => 2005
[2] => 03
[3] => 27
[4] => T00:00:00
[5] => 03:00
[6] => AM
)
I would feed those pieces into a DateTime object. Then you can output it into any format you want. You also may have to adjust that regex some so it can handle all your dates.
If the T00:00:00 part is useless, just remove it and use strtotime():
<?php
$date = '2005-03-27T00:00:00 03:00 AM';
$timestamp = strtotime(preg_replace('!T[^ ]+!', '', $date));
var_dump(date('d/m/Y H:i:s', $timestamp));
?>
This prints:
string(19) "27/03/2005 03:00:00"

How to display after 4 week date from now?I want to pass an argument

echo date( "F jS, Y" , strtotime("now +3 weeks") );
It gives the result as July 2nd, 2010 .
Fine.Now I want to pass the argument like this.
The original print_r($originalamount)
give the result like this
Array (
[0] => 4 Months
[1] => 3500
)
My code
$text=trim($originalamount[0]);
$text1="now +".$text;
echo date( "F jS, Y" , strtotime($text1)) ;
The out put come like this
December 31st, 1969
I don't know why.
alt text
I have just tried ...
$originalamount[0] = '4 Months';
$text=trim($originalamount[0]);
$text1="now +".$text;
echo date( "F jS, Y" , strtotime($text1)) ;
And it works fine...
October 11th, 2010
Can you please give us a var_dump of $originalamount is there any other code that might be messing with $originalamount

Categories