How to display date in specific format - php

I have a date field that I would like to express through a MySQL Query or as a PHP function as yyyymmdd, basically without any - or /, all the examples I can find include these.
I could build it a piece at a time, so y.m.d, but thought someone here might have come across this for, its basically a naming convention for network folders, hence why no special characters.
Thank you in advance

You can use PHP's DateTime Class:
echo (new DateTime($dateField))->format('Ymd');

SQL you can use DATE_FORMAT
DATE_FORMAT(NOW(),'%Y%m%d') //20150513
PHP you can use using date
echo date('Ymd'); //20150513

You can use strtotime() and date() function like this:
$time = strtotime($yourdatefield);
$date = date('Ymd', $time);

Related

How to format date-time for RFC2445 in PHP

I am working on Google Calendar API using its PHP library and I need to set a recurring event for which I have to create a RRule string which should be of following format:
RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z
I am unable to create the date in above format. I literally tried all methods like:
date('YmdHis');
date(DATE_RFC2822);
date('c');
But Google doesn't accept any of above formats. I need to make it like: 20110701T170000Z. Can anybody help me on this?
Thanks
This is your date format:
date('Ymd\THis\Z')
becomes
20150429T154315Z
Look at the examples here: http://php.net/manual/de/function.date.php
Are you able to concatenate two strings together and form the required datetime string in RFC2822?
<?php
$ymd = date('Ymd');
$hms = date('His');
echo $ymd."T".$hms."Z"; //Will output YYYYMMDDTHHMMSSZ
?>
Let me know if this works! :)
Try this:
date('Ymd\THis\Z', $timestamp);
or
date('Ymd\THis\Z');
if you want the current timestamp

Elegantly change 2011-10-31T16:22:00 to 2011-10-31 in php

I was wondering if anyone could help me out with this.
I am running PHP Version 5.2.16.
Up until now I have used a substring.
$Date = substr($Date,0,10);
$Date = mysql_real_escape_string($Date);
I am scraping this string from an REST Api, so I have no control over its format, and I am not sure if the 'T' in the middle of the string could cause a problem.
Now this does the job simply enough but I was looking to use something more elegant like
$Date = Date::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
but this just returns the error:
Fatal error: Class 'Date' not found in...
Im quite new to php but I am assuming that I require the Date class (Common sense) but how would I implement this class into my script?
Any help or suggestions would be greatly appreciated.
Thanks :)
You could do:
<?php
$Date = strtotime("2011-10-31T16:22:00");
$converted = date("Y-m-d", $Date);
echo ($converted);
?>
Look here http://codepad.org/TNIwXDRp
The class is called DateTime
$Date = DateTime::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
http://php.net/book.datetime
However, because the format is standardized (ISO 8601) it should not surprisingly change, so it should be safe to just split it into date and time yourself
list ($date, $time) = explode('T', $string);
The class is not called Date but DateTime and the createFromFormat() method requires PHP/5.3 or greater. Whatever, the format you have is correctly recognised by the constructor so all you need is:
<?php
$date = new DateTime('2011-10-31T16:22:00');
echo $date->format('Y-m-d');
You can either use substr to extract, or simply use strtotime. I have used both and they are both useful solutions. strtotime would be better if you also would need to extract the timestamp.

PHP Date to iCal date format for DTSTART

Is there an easy way to get the correct format for an iCal DTSTART using php date?
I need the format to look like: 20111008T110000 or 20111008 (that one is easy) if I don't have any time.
Does PHP date have a quick way to do this, specifically one that adds the time or removes it when needed?
There isn't any native PHP function or date format that I'm aware of, so you'll need to create your own function. Something like this:
function getIcalDate($time, $inclTime = true)
{
return date('Ymd' . ($inclTime ? '\THis' : ''), $time);
}
As hakre pointed out in a comment, the date formatter can't distinguish between a date with time and a date without time - you'll have to decide the logic behind that.
date('Ymd\THis', time())
You can replace time() with your own timestamp.
This is might be you are looking for link and this also can help you.
mktime function to make timestamp as you want including time or only date to it.

Date_format() ? Question help

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.

what is the best method to translate strings to mysql dates?

What is the best way in php to take the following string mm[some char]dd[some char]yyyy and translate it to yyyymmdd?
I will probably want in the future, according to local do the same with dd[some char]mm[some char]yyyy.
If there is a way that already uses the Zend Framework API, the better
<?php
$str = '08-24-1989'; // can be in any recognizable date format.
$new_str = date('Ymd', strtotime($str)); // produces "20090824".
?>
You can replace Ymd in the second statement above with any date format characters found here.
If you're looking to use Zend's Zend_Date framework, check out some examples and documentation here. Quite frankly though, the PHP functions are a lot simpler and easier to use in your case.
date('Ymd', strtotime($time));
Strtotime is absolutely the best tool to translate almost any time format into a standard one that you can then use Date to put into the format you want.
Because you question title says MySQL Dates, this is the string format that mysql uses.
date('Y-m-d h:i:s', strtotime($time));
Unless [some char] varies , use the mysql str_to_date function, e.g. STR_TO_DATE('12|23|2009','%m|%d|%Y');
I would absolutely use TIMESTAMP for any date storage. It's incredibly easy to handle time differences (like SELECT ... WHERE date BETWEEN 2138728753 AND 376251237) and can be translated to any locale pretty easily :)

Categories