strtotime not working - php

I am new to PHP and trying to convert a date from a registration form (in the form 01/01/2011) to Y-m-d so that it can be stored in the database.
This is the code I have, pretty sure it worked before but now it has stopped working. Any ideas?
$dateformat = $_POST['dob'];
$correctformat = date('Y-m-d',strtortime($dateformat));
I have checked what $_POST['dob'] is printing and it is correct, however the $correctformat is printing 1970-01-01 everytime.
Any help would be greatly appreciated.
Thanks

$currentformat = explode("/",$_POST['date']);
$newformat = $currentformat[2]."-".$currentformat[1]."-".$currentformat[0];

You could use DateTime::createFromFormat (note PHP 5 >= 5.3.0) instead of strtotime as strtotime acceptsm/d/Y not d/m/Y
try :
$dateformat = $_POST['dob'];
$correctformat = DateTime::createFromFormat('d/m/Y', $dateformat);
echo $correctformat->format('Y-m-d');

.1. Can you tell, looking at the date, if it have month or day first? If you can't - how it is supposed for the whatever program to determine the actual order?
.2. this operation require the very basic string manipulations anyone have to learn if planning to be a PHP user:
$chunks = explode("/",$_POST['date']);
$date = "$chunks[2]-$chunks[1]-$chunks[0]";
the order of chunks depends on the actual order of date parts

As Adam says, there is no strtortime() it should be strtotime()
Did you really test it with the literal string '01/01/2011'? Some of us were born before 1970 - which may be causing some confusion (strtotime() generates a 32 bit Unix time).
(beware that strtotime interprets xx/xx/xx[xx] using the americal style of mm/dd/yy[yy] rather than the european convention of dd/mm/yy[yy]).
It works for me with the typo removed:
[colinm#dev-sensor ~]$ php -q t.php
2011-01-01
[colinm#dev-sensor ~]$ cat t.php
<?php
print date('Y-m-d',strtotime('01/01/2011')) . "\n";

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

Why won't PHP strtotime work for a date in this format mdy?

I have a problem with verifying dates in PHP 5.2.17. I need the user to be able to enter the six digit date without any punctuation so Jan. 15, 2014 would be 011514. (This is due to input device limitation and for user convenience).
Tested and this is not working:
$testDate = "011514";
$myDate = date('mdy', strtotime($testDate));
echo $myDate;
I would expect that myDate is the same as test date but it's not, it prints out today's date 042314 (April 23, 2014)! PHP doesn't like that there is no punctuation in my original string. If I change $testDate = "01-15-14" then PHP spits out the correct string "011514".
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
Extra info:
PHP version is 5.2.17 so date_create_from_format is NOT able to be used.
There's a limit to what strtotime() can understand. It can't understand everything. That's why they tell us what it does and we, as developers, need to write our code in a way that respects that.
All you need to do in your code is insert slashes and you're all set.
$testDate = "011514";
$date = sprintf('%s/%s/%s',
substr($testDate, 0, 2),
substr($testDate, 2, 2),
substr($testDate, 4, 2)
);
$myDate = date('mdy', strtotime($date));
echo $myDate;
Demo
Answering to your only question
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
To get strtotime() to understand your format you need to download php sources, patch it so that it supported such a weird format and use your custom build.

PHP time() function returns the epoch instead of the current time

I have the following php code that is meant to format the current date and time:
$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
$this->debug($datetime);
The formatting seems to be working fine, but it keeps outputting that it is 1970; I get the following output:
1970-01-01T00:00:00.000Z
My guess is that my server is not configured properly, but my Google search has not given me any clues. I am running WAMP if it helps.
Thanks in advance for any advice you may have.
EDIT: it seems that the date and time functions are working properly; but assigning them to a variable is what is the problem. Any work arounds to get the same formatting as above would be welcomed. But I would also like to know why this problem is happening.
Firstly, you don't need to use time() at all here, because date() will use the current time as the default value if you don't pass a value to that parameter.
Secondly, you're using two separate date() calls, separated by a "T". Note that the formatting for date() can accept a hard-coded character like T; you just need to escape them with backslashes, so you don't need to split it into two function calls.
Your entire code could look like this:
$datetime = date('Y-m-d \T H:i:s.\0\0\0\Z');
Which gives 2013-06-19 T 11:18:53.000Z
It works perfectly for me (when echoing, instead of $this->debug). So either you somehow have a faulty PHP version, or the problem is not in your code sample. This is what I did:
<?php
$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
echo $datetime,"\n";

Is it possible to manipulate the date this PHP code outputs?

<?php echo invoice_due_date($invoice); ?>
The above code outputs a date that is formatted as dd/mm/yyyy. I am looking for a way to take that date and then add or remove x amount of days from it and then print the result. I'm a novice when it comes to PHP so everything I have tried has failed. Thanks in advance!
Also, this code snippet is from a no longer supported project called "myclientbase" if it helps.
PHP5 has a nice class called DateTime.
You can initialize it from a string like this:
$date = DateTime::createFromFormat('d/m/Y', invoice_due_date($invoice));
Then, since PHP 5.3.0 this class has a method to add time amounts:
$date->add(new DateInterval('P10D'));
This adds 10 days to your date. See http://php.net/manual/en/datetime.add.php.
strtotime can translate the date to a time value (although you'd have to replace the slashes with dashes) and in the same operation add days:
strtotime(str_replace('/','-',invoice_due_date($invoice)) . ' + 1 day')
date can be used to format the date back to a suitable notation.
You can use the DateTime::add function. A full explanation and examples can be found at http://php.net/manual/en/datetime.add.php
$invoice->add(new DateInterval('P20D'));
This would add 20 days to the invoice date. You can simply run this before echoing out your $invoice variable.

php/mysql getting date formats

i'm compiling a church register and the dates are in this format yyyy-mm-dd
example. 1978-03-27 .I need to make it 27th March,1978. Any php script to help me out?
I tried MSEXcel but messes it up. thanks (anyway..i'm getting it from a database) so i would be glad if i have it been read from a database or a file of dates.
Try with:
$input = '1978-03-27';
$timestamp = strtotime($input);
$output = date('dS F, Y', $timestamp);
Reference to date format: php.net
Try this:
$in = '1978-03-27';
$out = date('jS F Y',strtotime($in));
var_dump($out);
date() formats your output, and strtotime() converts your input to a timestamp. Keep in mind this will only work for values from 1970-01-01. If you want to work with "older" dates, you have to parse the input manually and generate the output you require.

Categories