I have a timestamp like 1397105576 and I need to convert it to data format. I used:
echo $today = date('20y-m-d H:m:s',"1397105576");
I am getting:
Severity: Warning
Message: date() expects parameter 2 to be long, object given
in the codeigniter framework.
update:
i found the answer
the vaiable should be converted to long
ie
echo $today = date('20y-m-d H:m:s',intval("1397105576"));
echo date('Y-m-d H:m:s',"1397105576");
Returns
2014-04-10 14:04:56
Update:
That should work in codeigniter also...however there is a CI function that does something similar as above:
$datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
$time = time();
echo mdate($datestring, $time);
More about this can be found at the user guide. You might be interested in the unix_to_human() function in particular.
It seems everywhere except for Russell Dias made the mistake of using m as minutes and not i
Y-m-d H:i:s NOT "Y-m-d H:m:s"
your code is working correctly although try it again like this:
<?php
echo $today = date('Y-m-d H:m:s',1397105576);
?>
[edit]
I'm going to leave my answer here as a matter of pointing out/teaching you the non-CodeIgnitor sanitized version of PHP's object/typing (+date function!) system, but I HIGHLY suggest using, and accepting, Russell Dias' answer.
Is your example 100% literal?
Is your line of code literally echo $today = date('20y-m-d H:m:s',"1397105576"); or is it something more like echo $today = date('20y-m-d H:m:s',"$date");?
If it's the latter, and the variable is being created by a class, then it may not be properly-typed.
You could probably use… I think an int would suffice here, I think you could do something like echo $today = date('20y-m-d H:m:s',(int)$date);, again, assuming that your line of code is not in-fact literal.
I'm not 100% sure that int would work, if it doesn't, (float) is the only other numeric type that possible could. Perhaps (string)?
See Type Juggling.
As an aside, why are you using 20y? Please change that to a Y
From PHP's Date Docs:
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003
y | A two digit representation of a year | Examples: 99 or 03
Related
I'm working with an XML document that is returning variables and for some reason in a xml return the timestamp is formatted like this... 20180606T110000 ... why anyone would format it like that makes no sense to me; however, its what I have to work with. ITs formatted YYYYMMDD , the T is the split between date and time, HHMMSS. ITs set up in a 24 Hour clock that I also need to convert to 12 hr clock with am/pm
I need that formatted like 06/06/2018 11:00:00 AM.
Is there a way to do that via a date format (I know how to use date() but I don't know how to bring in that timestamp the way its formatted) or even separating it out into
$year = xxxx
$month = xx
$day = $xx
$Hour=xx
etc. etc. etc.
if need be.
I've briefly looked at php's date create from format ( date_create_from_format('j-M-Y', '15-Feb-2009') ) but dont fully understand how that works.
I've also thought about a split. I've also looked at chunk_split and wordwrap but its not even amounts of characters so that would be complex to create.
Any ideas?
The format you're working with is "XMLRPC (Compact)" format. This is fully supported by PHP (you can see a list of supported formats here). To get what you want, just use a combination of strtotime() and date().
$timestring = "20180606T110000";
$timestamp = strtotime($timestring);
echo date("m/d/Y h:i:s A", $timestamp);
You can use PHP DateTime to parse a datetime String with any format. Please view the Parameters format in the following link to understand how the "Ymd\THis" part works: http://php.net/manual/en/datetime.createfromformat.php
<?php
$time = "20180606T110000";
$date = DateTime::createFromFormat("Ymd\THis", $time);
// 06/06/2018 11:00:00 AM.
echo $date->format("d/m/Y h:i:s A");
I am trying to get current time and date in order to echo it out on my website. I have the follow snippet:
$date_of_msg = date("Y-m-d");
$time_of_msg = time();
When I echo $time_of_msg I get 00:00:00. I have tried to edit my code based on this solution here but which this approach, when I echo the variable, I get 838:59:59. I simply want the current time to be displayed in 24 hour format.
In addition to this, I currently have the date formatted to (Y-m-d), which is great because it works. I am trying to format it so that it displays day, number, year, i.e. today is 20th Feb, so I want the date to display Feb 20, 2016. I have tried the following based on documentation (see here)
$date_of_msg = date("F j, Y")
But again, the date displays nothing. Am I missing something?
If you need the current time in the 24h format just use
$time_of_msg = date("H:i");
The date part seems correct that way, you must be doing something wrong while displaying it.
time() (unless you override it in some weird fashion) gives you a timestamp, i.e. the amount of seconds which have passed since 1970-01-01 until now. date(), however, gives you a string representation of a date, which may or may not include the minutes and seconds - depending on how you choose to format it.
So, if you want to display the time and date to a user, you should probably go for something like
$date_of_msg = date("F j, Y H:i:s")
The documentation on date() gives you an excellent description of available options.
time() returns a UNIX timestamp while date() format a timestamp. Your call date("Y-m-d") means the same as date("Y-m-d", time()).
Even though the function is called date(), it can also format time. You just have to use the correct placeholders. E.g. date("H:i:s") would give you a 24h-time like 17:43:23.
This code
<?php
echo date("Y-m-d").PHP_EOL;
echo time().PHP_EOL;
echo date("F j, Y").PHP_EOL;
Returns this result, as expected
2016-02-20
1455927480
February 20, 2016
So what are you doing that you are not actually telling us
Hi I am struggling with converting a string:
$file = '150220';
into a mysql date format.
I have tried this
$newDate = date("Y-m-d", strtotime($file));
But it returns the following 2015-02-24 (24 being today's date)
See: http://php.net/manual/en/datetime.createfromformat.php
So you can use this:
$dateTime = DateTime::createFromFormat('ymd', $file);
$newDate = $dateTime->format('Y-m-d');
strtotime is a wonderful function. It works when you provide YYYY-MM-DD format but not with YY-MM-DD.
EDIT: As said in comments, you have to "turn the string into a valid ISO8601 Notations format by prepending '20' - this is understood by strtotime as specified in the docs "
This will work for the next 85 years with 64 bits systems (Until 2038 on 32 bit systems)
$file = '150220';
$newDate = date("Y-m-d", strtotime("20$file"));
You have to be careful with strtotime because it returns false on error which will make date used today's date.
EDIT: if you want to -1 : first, this code works and it explains why op's code does not work, then consider using the "Add comment" button. Thanks.
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
$t = DateTime::createFromFormat('Gi', '900');
$time_str = $t->format('gi a');
echo $time_str; //outputs 600 pm instead of 9am. Why? and How do I get 9am?
I am not sure where I am going wrong.. I am following what is given here in terms of date formatting:
http://php.net/manual/en/function.date.php
Thanks!
The documentation you linked is for the date() function. The DateTime::createFromFormat is not the same (though the format strings are pretty much identical).
I expect the format parsing is having trouble recognizing the difference between the hour and minute components.
If you split them up with a space, you get the desired result:
$t = DateTime::createFromFormat('G i', '9 00');
$time_str = $t->format('gi a');
echo $time_str;
// Output is 900 am
Edit:
The inability for PHP to parse a format string like Gi is a known bug. The parser for G doesn't know whether to read 9 or 90 and in the latter case that 90 is too high.
As i said in my comment this works :
$t = DateTime::createFromFormat('G i', '9 00');
$time_str = $t->format('gi a');
echo $time_str.PHP_EOL;
Cannot find any where written down - but suspect the time needs to be separated ... either by space or colon or something else