PHP Spreadsheet_Excel_Reader always reading date as Nov 30, 1999 - php

I am using Spreadsheet_Excel_Reader to read .xls files in PHP.
Everything goes fine until it comes to reading a date. If I am reading a date field, it will always return the date as Nov 30, 1999 (or variations of this date depending upon the format). I have tried setting the OutputEncoding and it's giving the same result. I tried dumping the 'example.xls' that comes with the library and that also produces the same result.
Any help on a workaround for this would be highly appreciated.

You don't need to format your date in excel...if you have a date format in mind wrap it with double quote. e.g "13/04/1987" format("DD/MM/YYYY");
Spreadsheet_Excel_Reader will read this as a normal string with the double quote wrapper around it.
Then in your PHP make a query to remove and replace double quote with nothing.
$var = "13/04/1987";
$removeQuote = str_replace('"','',$var);
After this you will have to replace every occurence of the forwardslash (/) with hypen(-).
$removeSlashes = str_replace('/','-',$removeQuote);
Then use the date function in PHP to format it to your suitability.
$format = date('Y-m-d', strtotime($removeSlashes));
echo $format;
And you'r done... full code goes below.
$var = "13/04/1987";
echo date('Y-m-d',strtotime(str_replace('/','-',str_replace('"','',$var))));

Related

How to separate the timezone and datetime from a string?

I have a string get from a text file name.
$string = "20181011000000GMT+0800"
// trying to convert it
Carbon::createFromFormat('YmdHisTO', $string)->format...
It shows me this error.
Unexpected data found. The timezone could not be found in the database
Data missing
How can i separate the datetime and timezone, if there's GMT+0800 end of the datetime string?
You can parse that format with a slight change to the format string you're using:
$string = "20181011000000GMT+0800";
Carbon::createFromFormat('YmdHisT+', $string);
The + sign tells the parser to ignore everything after the first timezone identifier (GMT). Strictly, it'll work without the + too, but I prefer to use it for clarity. Up to you though.
See https://3v4l.org/kDorn
Actually, testing this a bit more, I'm not sure the plus sign is that relevant. I think either of the timezone format characters (T and O) are simply swallowing all of the timezone information, rather than just their specific parts. DateTime's parser can be a bit of a minefield sometimes.
I would recommend using strtotime() it accepts every valid datetime format. See usage: http://nl1.php.net/manual/en/function.strtotime.php
$DateTimeString = "20181011000000GMT+0800";
$timestamp = strtotime($DateTimeString);
// trying to convert it
Carbon::createFromTimestamp($timestamp)->toDateTimeString();

PHP - Find file in Directory based on date /YEAR/MONTH/DAY - But not based on current date - static Datestamp

I have a variable pulled from a SQL DB which is in the "datetime" format of:
2017-02-22 16:24:12
I will be needing to find a file in this directory based on the results of the remaining code, but im not sure how to format the date variables for the directory.
Eg:
/basefolder/2017/02/22/File.extension
I have read lots about using date variables, but they are all based on the current date. My thoughts were that I could just strip the numbers like
/basefolder/"first 4 numbers"/"second 2 numbers"/"last 2 numbers"/
VS trying to do this with any sort of date/time functions. But perhaps there is a date fuction I can/should use.
I do know that the datetime will always be formatted as such, but it feels "hackish" to do it the first way. I could do it in bash in a second, but I'm not so good w/ PHP.
Thanks for any help!
PS: To give context, I will be getting 2 variables passed to this php script, phone number and date. I will then query the CDR table for the Unique ID of this call record based on that information, then I know the file will be in:
/monitor/2017/02/22/*uniqueID*
Which I will then pass back to the original script to have it download the file.
You can use the function strtotime to convert your date string to a timestamp, and then use the date function to create the date formatted as you like, and you can pass the timestamp as the second argument (it only uses current time if you pass only the format)
docs for strtotime: http://php.net/manual/en/function.strtotime.php
<?php
$dateString = '2017-02-22 16:24:12';
$timestamp = strtotime($dateString);
$datepath = date('Y/m/d',$timestamp);
$path = "/basefolder/$datepath/";
var_dump($path);
This outputs: string(20) "/basefolder/2017/02/22/"
This code could be shortened to:
$path = "/basefolder/".date('Y/m/d/',strtotime('2017-02-22 16:24:12'));
It sounds like you already found the date function but there is more info on it here: http://php.net/manual/en/function.date.php
Note that you have to do it in two stages, as if you include the /basefolder/ part in the format passed to date, then it will replace individual characters with various formats of the date - e.g.:
$datepath = date('/basefolder/Y/m/d',$timestamp);
Sets $datepath to something like bpm12US/Pacificf2017Wednesday22US/PacificWed, 22 Feb 2017 16:24:12 -0800/2017/02/22
b is left alone because it means nothing, but then a is replaced with pm because of the time, s is replaced with the seconds value, e is replaced with the time zone and so on.
date_parse_from_format() will convert from string to broken down time array:
<?php
$d = date_parse_from_format("Y-m-d H:i:s", "2017-02-22 16:24:12");
$ds = sprintf("/base/%d/%02d/%d", $d['year'], $d['month'], $d['day']);
echo $ds;
This is my final code snippit!
<?php
$dateString = '2017-02-22 16:24:12';
$base = "/monitor/";
$path = "$base".date('Y/m/d/',strtotime("$dateString"));
echo $path;
?>

PHP mktime notice

I want to change given date and time or date only into Unix time.
I tried like this:
mktime("Jan-12-2012 2:12pm");
But it’s not working:
Even in PHP documentation I looked at many examples and many of them don’t consist the matter that I want.
And when I try:
$user_birthday=$_POST["user_birthday"];
$db_user_birthday=empty($user_birthday)?"":mktime($user_birthday);
$_POST["user_birthday"] was given value from form that is jan-12-2012 2:12pm
it show error like this:
Notice: A non well formed numeric value encountered in C:\Program
Files (x86)\Ampps\www\admin\index.php on line 76
How do I fix it or display time into Unix?
Use this one:
date("M-d-Y h:i:s", strtotime($user_birthday));
You should be using strtotime instead of mktime:
Parse about any English textual datetime description into a Unix
timestamp.
So your code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
Then you can process that date like this to get it formatted as you want it to:
echo date("M-d-Y h:ia", $db_user_birthday);
So your full code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
echo date("M-d-Y h:ia", $db_user_birthday);
Note I also added spaces to your code in key points. The code will work without the spaces, but for readability & formatting, you should always opt to use cleaner code like this.
You should take a look at this answer: convert date to unixtime php
Essentially, you have mixed up mktime() with strtotime(). strtotime() allows you to parse an English textual string into a Unix timestamp. mktime() constructs a unix datetime based on integer arguments.
For example (again taken from the question above)
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
echo strtotime("2009-11-03 11:24:00PM");
1257290640

PHP Time Problem

well, I"ve got a time format of the type: 2011-02-16T01:25:50+04:00
I want to strip the +04:00 from the end of the string. How to do it?
I am converting a timestamp from mysql to ISO 8601 format using this function:
$timeagotime = date('c',strtotime($row['created_at']));
where $timeagotime contains the value: 2011-02-16T01:25:50+04:00
Refer to the documentation for php's date function: http://php.net/manual/en/function.date.php
You need to use a different format string. For instance,
date('Y-m-d',strtotime($row['created_at']));
Givens you "2011-02-16". There are a number of format strings... check out the doc.
You could use substr() to remove it, or just generate a date without it in the first place, though you couldn't use the 'c' formatting option, since that'd just add the timezone on again.
$timeagotime = substr($timeagotime, 0, 19)
$timeagotime = date('Y-m-dTG:i:s', strtotime(...));
If you absolutely don't need the end then you can use the following. Otherwise I'd suggest using a different way to generate your date from the table. PHP Date
$timeagotime = substr(date('c',strtotime($row['created_at'])), 0, 19);

why am I getting '4:00' when I'm trying to display time using the date function with php?

I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.
Here is my code:
date('g:i', strtotime($row['startTime']))
An example of I have the time displayed in my database is like this: 00:12:30
Why is it showing '4:00' every time?
strtotime expects a datetime format ... you should do
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).
strtotime converts a date time string to a Unix timestamp.
Perhaps your $row['startTime'] doesn't qualify as a date time string.
None of the examples here discussed a date time string which did not include a date.
The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.
As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";

Categories