php


British Date Formatting in PHP


<?php
$firstdate = '2011-11-16';
$sample = date("d-m-Y", strtotime($firstdate));
echo $sample;
?>
I have a column in a table with date in this format 2011-11-16
But I need it in reverse, so I've used the following code.
The problem is that when the date is 0000-00-00, it prints 01-01-1970.
Is there a way of making it print 00-00-0000 instead? Please note that I'm looking to reverse it to British format. 00-00-0000.
Thanks
The problem is in the way dates are represented internally. They use what's called Unix time, in which the date is represented as a number of seconds after the "Unix epoch", which is 1/1/1970. This is why the date function (correctly) represents 0 (which is what strtotime returns when you pass it 0000-00-00) as Jan 1, 1970.
The only solution I can think of is something like this:
<?php
$firstdate = '2011-11-16';
$sample = $firstdate == '0000-00-00' ? '00-00-0000' : date("d-m-Y", strtotime($firstdate));
echo $sample;
?>
You can do something like that :
<?php
$firstdate = '2011-11-16';
$sample = $firstdate!=='0000-00-00'?date("d-m-Y", strtotime($firstdate)):''00-00-0000;
echo $sample;
?>
When you get the empty date,reverse it.
if( $firstdate == '0000-00-00') $sample = strrev( $firstdate);
else
{
$sample = date("d-m-Y", strtotime($firstdate));
}
0000-00-00 is not a valid date. Obviously there's no month 0.
So if you use that as a placeholder for 'no date at all', then the solution is rather simple:
if($date==='0000-00-00') {
echo '00-00-0000';
}

Related Links

Using ODBC Driver 13 instead of ODBC Driver 11 to Connect to SQL Server 2008 R2
Overload a bundle with Symfony2
Store php variable in table on mysql
No data submitted from a form
twilio error - message.json was not found
Remove two characters all occurrence in a string using a pattern
How to read data from nth sheet of Excel sheet in php?
Ad Preview of All Facebook Ads under an account
How to enable php curl extension
How load a table into multiple webpages using HTML?
Woo commerce how to display child category titles along with the parent one?
Active Collab SSL related error
Codeception\Exception\TestParseException Couldn't parse test
php preg_match not working as in other web applications
When try to reindexing in magento2 by ubuntu terminal I am getting this(Access denied for user 'root'#'localhost'(using password: NO)) error
Datatables and slow Ajax sourced data

Categories

HOME
hpoo
functional-programming
angular-ui-grid
dotnetnuke
livecode
scroll
websocket
mongoid
elisp
hyperledger
turbojpeg
ipv6
bittorrent
gprs
eddystone
stack-overflow
bundler
fresco
tfsbuild
cpu-usage
bluemix-mobile-services
social-tables
android-permissions
sqlplus
bosun
gollum-wiki
conda
window-managers
fstar
crud
jackrabbit-oak
onchange
tsung
microprocessors
dnsmasq
basic-authentication
copy-protection
caesar-cipher
cloudera-manager
subclassing
xll
mars-simulator
classnotfoundexception
vb.net-to-c#
1wire
optionaldataexception
httphandler
expandablelistview
spongycastle
realstudio
uiautomatorviewer
beeline
ilrepack
exc-bad-access
clrs
jsonix
data-management
nunit-console
wolfram-language
android-sdcard
deferred-rendering
noclassdeffounderror
codecharge
rails-engines
apple-configurator
mathnet
persistent-object-store
vimperator
openmrs
jsr363
boost-proto
ruby-on-rails-4.1
apache-commons-digester
extend
dbmigrate
maven-shade-plugin
createfile
fat32
juttle
n-tier-architecture
rubber
cout
ivalueconverter
flipboard
proxy-server
facebook-game-groups
insertonsubmit
insertion
chefspec
itunes-sdk
gprof
live-wallpaper
pageheap
jdom
struts2-s2hibernate
kolite
will-paginate
motodev-studio
pyjamas
ccnet-config
facebook-fbml
bucket
wmd-editor
main-method
sqlsitemapprovider
rakudo
account-management

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App