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

How to make a username entries required to be unique with php
Adding a link to click a new button on the form [duplicate]
How to bypass CloudFlare rocket script if wp_enqueue_script is used to add scripts?
Image path upload and displaying image in mysql
fusionchart in php are not showing
php built in webserver not reacheable with ip:port via network cable
I cant get value of the radio button Except first radio button
Edit iframe content using PHP, and preg_replace()
PMPRO Wordpress plugin Membership Cancellation Errors (stripe)
Android HttpURLConnection Post Data, PHP App Server can't receive data
compare todays date with the date in the mysql table using codeigniter
Get all possible combinations without duplicates
how to upload to files to amazon EC2
Getting the top scores, but remove duplicate users (SQL)
Explode a string after 5 spaces and 2 new lines in a string in PHP
How to create a button that links to different URL using if-else statement

Categories

HOME
firebase
plesk
matrix
adfs3.0
transactions
windows-10
compilation
theano
ios-simulator
tabs
jersey-2.0
web2py
mirc
repair
port
swarm
lc3
lapack
nsstring
rest-assured
string-formatting
chronicle-queue
cell
backendless
vuforia
password-generator
openbr
steam-web-api
wampsharp
caesar-cipher
kendo-listview
winrm
rails-postgresql
cargo
extbase
plane
git-tfs
sony-future-lab-n
scala-breeze
renderman
gtk#
freshdesk
scala-collections
word-2007
front-camera
cognos-tm1
oid
msg
coovachilli
xcode8-beta4
openseadragon
node-glob
simian
adjacency-list
polymerfire
wikimedia-commons
deeplink
pass-by-value
uno
sunos
visual-c++-2012
windows-messages
csslint
stripe.net
forceclose
meteorite
frontbase
application-server
yii-booster
spiral
towers-of-hanoi
jdom
mkannotation
n-queens
libxslt
gaelyk
aptitude
reliability
spring-modules
powergui
gtktextview
watchpoint
jvm-bytecode

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