Date_format() ? Question help - php

If I have a MySQL table field of my one date with this format (09-13-2011 11:22:43), as I do for PHP to print with this format (09/13/2011 11:22:43) Sorry my ignorance I searched the php.net site but can not find something about my request, Apologies.

$mysql_date = '09-13-2011 11:22:43';
$date = DateTime::createFromFormat('m-d-Y H:i:s', $mysql_date);
echo $date->format('m/d/Y H:i:s');

Use:
date( "m/d/Y h:i:s", UNIX_TIMESTAMP($fieldname));
If your field is already timestamp use the following:
date( "m/d/Y h:i:s", $fieldname);

I may be wrong in saying this but I don't think theres a standard way in doing so, unless you want to save the date/time as a unix_timestamp. If you do then you can format the time in however you want using the php date() function. If you aren't then you can always use something like str_replace() on the times to get them to the format you want or even use regex if your feeling adventurous

MySQL's DATE columns format is fairly irrelevant. Just use DATE_FORMAT() to convert the date to a string that suits your needs.

Related

separating a weird formatted timestamp into an understandable date

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");

How to convert ISO8601 to Date format in php

How to convert this (in ISO8601 format): 2014-03-13T09:05:50.240Z
To this (in MySQL DATE format): 2014-03-13
in php?
try this
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
The complete date function documentation can be found here: http://php.net/manual/en/function.date.php
The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.
Hope I could help :)
P.s.:
Just in case strtotime will return 0 try using this:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):
$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d'); // MySQL datetime format
There is no reason to use the inefficient time functions. The most efficient way is to simply extract the first 10 characters:
substr($date,0,10)
People, that are really coding for year ≥10000, can use:
substr($date,0,strpos($date,"T"))
Simply convert datetime description into a Unix timestamp using with strtotime and then five format using Date Formats
Try it will surely work for you.
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
For those using Carbon (php library), the parse() works quite well:
Carbon::parse($date)
https://carbon.nesbot.com/docs/
Today I have published an interitty/utils package that deals with, among other things, the ISO-8601 format and perhaps all permutations of this standard.
I hope it will help you too.
$dateTimeFactory = new Interitty\Utils\DateTimeFactory();
$dateTime = $dateTimeFactory->createFromIso8601('1989-12-17T12:00:00Z');

Convert mm/dd/yyyy into mysql timestamp?

I've been trying for about two hours to get this working with no luck. I'm trying to convert a date that's entered like 11/18/2012 into a mysql timestamp but everything I've tried just ends up as either 0000-00-00 00:00:00 or NULL in the database :( I'm using PHP + MYSQL so a solution in either would be great
Try This
$release_date=$_POST['release_date'];
echo date("Y-m-d H:i:s",strtotime($release_date));
PHP's DateTime to the rescue!
$datetime = new DateTime($alternativeFormat);
echo $datetime->format('Y-m-d H:i:s'); // mysql format
It's also possible to leave the altering of the data by MySQL, but I advice against it. By using the DateTime object you leave your query open to support other formats aswell.
Use STR_TO_DATE:
SELECT TIMESTAMP(STR_TO_DATE('11/18/2012','%m/%d/%Y'))
http://sqlfiddle.com/#!2/d41d8/4363/0
You stated that you tried:
UNIX_TIMESTAMP(STR_TO_DATE('$release_date','%m/%d/%y'))
The reason this doesn't work is because UNIX_TIMESTAMP's return type is unsigned integer, not TIMESTAMP. This is why you can't insert it into a TIMESTAMP column
Have you tried strtotime(): http://php.net/manual/en/function.strtotime.php
Or exploding on '/' and then using mktime(): http://php.net/manual/en/function.mktime.php
$parts = explode('/', $date);
$timestamp = mktime(0, 0, 0, $parts[0], $parts[1], $parts[2]);
Or any of the other suggestions in answers posted here?
My favorite method:
$date = '10/1/2012';
$mysqlDate = date('Y-m-d', strtotime($date));
I would try something like this.
$sDate = '11/18/2012';
$aDate = explode('/', $sDate);
$sMySQLTimestamp = sprintf(
'%s-%s-%s 00:00:00',
$aDate[2],
$aDate[0],
$aDate[1]
);
var_dump($sMySQLTimestamp);
> string(19) "2012-11-18 00:00:00"
The correct answer will depend upon exactly what you're trying to do, but in most cases it is a combination of these things:
Use a DateTime object, rather than a string, to represent the timestamp in PHP. (Convert it on input rather than when writing to the database).
Use a database interface that has placeholders, and when filling in a value that's a DateTime, automatically converts them to the appropriate string format
This keeps you from having to convert to, or even know, the native format expected by MySQL.
can you enter that example date as 2012/11/18 ?
if yes use
select convert('2012/11/18' ,DATETIME)
or you can use
select convert(str_to_date('11/18/2012','%m/%d/%Y'),DATETIME)

Formatting datetime

i am trying to format a datetime which comes fromt he database in the format of
2012-06-11 21:39:54
However i want it to display in the format of June 11
How can do this?
Thanks
echo date('M d', strtotime('2012-06-11 21:39:54'));
Output
You can also use DateTime object.
$date = new DateTime($yourString);
$date->format($yourFOrmat);
I think that it would be the best way because DateTime is really more powerful than timestamp and date/strtotime functions.
From the code I gave above you can add functionalities like modifying dates, iterate over the time, compare 2 dates without functions like str_to_time...
$date->modify('+1 day');//the day after for example
foreach(new DatePeriod($date,new DateInterval('PT1M'),10){
$date->format($yourFormat);//iterate each minute
}
and so on
PHP manual gives an excellent documentation about using Date/Time functions. Basically you will need a combination of two functions: strtotime() and date().
strtotime() will convert your date into Unix timestamp which can be supplied to date() as second argument.
The format of date you will need is: M d.
Alternative: In addition you could also try the MYSQL counterpart which won't require conversion to UNIX timestamp. It is documented here. Assuming you are using date as your Datetime field, you will need something like this,
SELECT id,..,DATE_FORMAT(`date`, '%M %d') as f_date FROM table
For formatting date using php, you need to pass timestamp of date
and format specifiers as arguments into date function .
Eg echo date('M d',strtotime('2012-06-11 21:39:54'));

Convert uncommon date format to timestamp in most efficient manner possible?

I have a date in this format: 20101101120000
I need to convert it to a timestamp with PHP.
I've been searching the PHP docs online, but can't find anything that can convert directly. Does one exist? If not, what's the most efficient way to do the conversion? Thank you for your help.
You can do this with DateTime::createFromFormat:
$d = DateTime::createFromFormat('YmdGis', '20101101120000');
$d is now a DateTime instance. You can either convert it to a timestamp with $d->getTimestamp() or use the DateTime methods on it.
Note that this requires PHP 5.3.
strtotime('20101101120000')
....
You need the function strptime.
The formats are described at strftime.
I only add to this resolved question because this may be helpful for users who stumble upon here (like I did) and are looking to get an actual datetime timestamp.
My assumption for most folks when they mention timestamp is that they're looking for a normal "YYYY-MM-DD HH:MM:SS" timestamp not a unix timestamp which you get when you use the getTimestamp() method (see accepted answer if you are indeed looking for the UNIX Timestamp).
So I will further elaborate on lonesomeday's answer (which is fully correct) for those looking to actually get a valid formated date that they can display to users or insert into their database:
$d = DateTime::createFromFormat('YmdGis', '20101101120000');
$formatedDate = $d->format('Y-m-d H:i:s'); // will return 2010-11-01 12:00:00
$formatedDate = $d->format('m-d-Y h:i A'); // will return 11-01-2010 12:00 PM

Categories