Decoding date from integer in PHP - php

In PHP, I'm saving a date record as string in order to make conditions like "more than", "less than"; but I would really like to know how to decode it. What I want to do is something like this:
The moment that record is being saved is 2014, 08/06 1:30 in 24-hours format, so my integer should be like 201408060130, of course for this I use date() function.
But when it comes to decoding it to show it back like 2014, 08/06 1:30 or another format like 08/06 2014, 1:30 I really get stuck thinking on any solution for this.
I thought it would be like:
$date = date('YdmHm'); //Saving as 201408060130
$this->saveToDatabase($save, $mytable);
$dateDecoded = $this->getFromDatabase($mytable, $id, $theDateInteger);
$result = Date::getFormat($dateDecoded, 'YdmHm'); //Decode date format
echo $result->date('Y d/m, H:m'); //Show 2014 08/06, 1:30

This is simple & works. Just use strtotime:
$test_value = '201408060130';
echo date('Y d/m, H:i', strtotime($test_value));
The output is:
2014 06/08, 01:30

PHP has a really useful method under the Date class for this called DateTime::createFromFormat()
Here is an example usage:
$datetime = DateTime::createFromFormat('YdmHm', (string)$theDateInteger);
Now this is a valid datetime object which you can save to any formatting in a string if desired. For example:
echo $datetime->date('Y d/m, H:m');

Related

Echo date in a prettier way

I'm not too good with dates in php (or any other language) and would need some help to convert a date's format into something more readable.
The string currently looks like this:
2021-03-31T00:00:00.0000000+02:00
But i would much rather prefer it to be similar to this when echo it:
2021-03-31 00:00:00
I have done some more or less useless stuff with string manipulation, but there must be a better way?
Example:
substr('2021-03-31T00:00:00.0000000+02:00',0,10);
How would one do this?
PHP has pretty extensive DateTime functionality that you can take advantage of, for this example you can use the DateTime class or DateTimeImmutable class.
$date = new DateTime('2021-03-31T00:00:00.0000000+02:00');
echo $date->format('Y-m-d H:i:s'); // 2021-03-31 00:00:00
You could also do it with string manipulation by
spliting string (e.g $datetime)with letter T to array by function explode
take first element and store it in a variable $date
take the second element of array and store it in $time (only 8 letters which represent time)
$datetime = '2021-03-31T00:00:00.0000000+02:00';
$date_time = explode('T', $datetime);
$date = $date_time[0];
$time = substr($date_time[1],0,8);
echo $date.' '.$time;//2021-03-31 00:00:00

Get Hour and minute from time string

I have response from an API like below
2019-05-08T10:36:00+0530
I want to take 10:36 from it, right now I do something with regex like
preg_match('/T(.*?)\:00/');
It works, but is there any other actual way to do it more efficiently? That works on any time string in this format?
Create a DateTime object by passing that string into the constructor and just format it to your liking with format()! You might want H:i if you just want the hours and minutes, or H:i:s if you want to include the seconds.
$string = "2019-05-08T10:36:00+0530";
$date = new DateTime($string);
echo $date->format("H:i:s");
Live demo at https://3v4l.org/BmnoU
If your string format is fixed, you can use substr().
$string = "2019-05-08T10:36:00+0530";
echo substr($string, 11, 5);
Output:
10:36
Working Example:

Codeigniter - human_to_unix correct input format?

I have a form, which includes a date field.
The date will be received in the format dd-mm-yyyy (e.g. 08-11-2015).
I want to convert this to a unix timestamp before inserting into the database.
If possible, I would like to use Codeigniter functions rather than native PHP functions.
Codeigniter has the function:
human_to_unix($input)
The function does not accept a format string and I cannot find the correct format to use ( how will it now that 08-11-2015 is 8th March 2015 rather than 11th August 2015??).
Thanks
The default date_helper in CodeIgniter won't accept dates in the format dd-mm-yyyy. You can use standard PHP (although you specified you would prefer CodeIgniter functions):
$date = DateTime::createFromFormat('d-m-Y', '08-11-2015');
echo $date->format('U');
Alternatively, you can extend the helpers that CodeIgniter provides.
Create a new file application/helpers/MY_date_helper.php:
<?php
function human_to_unix_alternative($datestr) { // change this to a suitable function name
$date = DateTime::createFromFormat('d-m-Y', $datestr);
return $date->format('U');
}
You can then use it in your code, just as you'd use human_to_unix($input), e.g. human_to_unix_alternative($input).
U have to use date format like this to get
human_to_unix -> YY-MM-DD HH:MM <space> meridian
ex:
$this->load->helper('date_helper');
human_to_unix("2015-12-03 03:28 PM");
outputs:
1449136680
$this->load->helper('date');
$input = strtotime('08-11-2015');
$human = unix_to_human($input);
$unix = human_to_unix($human);
Output will be:
2015-11-08 12:00 AM

How to translate AM and PM in PHP date

I have a context in a forum settings that use date function to handle datetime string. This context accept the format string of the date function. However, the forum in the Arabic language and I want to translate the string of am and pm to another equivalent in an Arabic string such as ص and م respectively. I tried the following code to make the external (the context) date() to replace:
echo date("addslashes(date('D d M Y g:i'),'A..Z')".str_ireplace(array('pm','am'), array('م','ص'),date(' a')));
I used addslashes in hope to escape the output of the first inner date. However the output is something like that:
pm202005Sundaypm0509UTC05(20pm30UTC('Sun 20 Apr 2014 9:03'),'PM..0') م
It is just scceeded in replacing am and pm but the whole date string is missy as you see. This is a live demo of the code. I need to know does it possible to get what I need, assuming that any code will be done inside a predefined date()?
You can use a ternary operator inside the date to set a conditional format.
date($format = (date(G) > 11) ? 'h:i \ص' : 'h:i \م');
or for am/pm
date($format = (date(G) > 11) ? 'h:i \p\m' : 'h:i \a\m');
You can set the format different than h:i but this will show 05:40 pm for 17:40 or 05:40 am for 05:40. It looks for date(G) which is the hours in 0-23 format. If date(G) is greater than 11, it uses the first format, else uses the second format.
Here is a fork of your code http://ideone.com/BobelH
There is another approach. It uses strtotime and str_replace as follows:
<?php
$dtFormat = "Y-m-d H:i:s"; //MySQL Datetime format
$curDT = date($dtFormat);
$curTime = strtotime($curDT);
$nowFormat = "Y-m-d h:i:s ";
$arrEn = array('am', 'pm');
$arrAr = array('ص', 'م');
echo date($nowFormat).str_replace($arrEn, $arrAr,date("a", $curTime));
A working demo: http://ideone.com/xfECCD

Passing a UK date in a URL

I am passing a date in a URL in a UK format as per the following:
http://www.website.com/index.php?from_date=01/04/2013&to_date=12/04/2013
The date range is 1st April 2013 to 12th April 2013.
Then I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$rpt_from_date = date("Y-m-d", strtotime($_GET["from_date"]) );
$rpt_to_date = date("Y-m-d", strtotime($_GET["to_date"]) );
echo $rpt_from_date;
echo "</br>";
echo $rpt_to_date;
?>
For some reason this is not working. The above returns the following:
2013-01-04
2013-12-04
It's switching the month and day around. I want it to return:
2013-04-01
2013-04-12
Does anyone have any ideas?
Use DateTime object, to get php understand in which format you passing date to it.
$rpt_from_date = DateTime::createFromFormat('d/m/Y', $_GET["from_date"]);
echo $rpt_from_date->format('Y-m-d');
PHP is reading your time string in the US format (MM/DD/YYYY) because you are using slashes. You could use dashes to give the time: index.php?from_date=01-04-2013&to_date=12-04-2013, or convert it yourself:
$uktime = implode("-", explode("/", $_GET['from_date']));
I will provide the solution but it is not using the date function:
$arr = explode("/",$_GET["from_date"]);
$from_date = $arr[2]."-".$arr[1]."-"$arr[0];
Second solution is as following:
$from_date = implode(array_reverse(explode("/",$_GET["from_date"])));

Categories