Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strtotime syntax problem I think. Not getting expected value 1

Status
Not open for further replies.

topjimmie

IS-IT--Management
Feb 4, 2002
28
US
I have a POSTed key of hidtimestamp. Naturally, the value of this key is a string.
Code:
print_r($_POST['hidtimestamp']);
Returns: 20100128_18:02

What I want is a DATE object like: 20100128_1802

So, I tried this:
Code:
$adate = date ('Ymd_Hi', strtotime($_POST['hidtimestamp']));
echo $adate;
Which returns: 19691231_1800

Could someone please tell me what I'm doing wrong and how I can get my string as a date obj, as well as format it?

Thanks!!
 
Hi jbmoog,

The function expects you to pass the datetime like this.

Code:
<?php

$datetime = '20100128 18:02';

$adate = date ('Ymd_Hi', strtotime($datetime));
echo $adate;

?>

Should format it to your requirements.

Regards,

Peter.

Remember- It's nice to be important,
but it's important to be nice :)
 
Thank you, but still not getting anything expected.
It's really odd, but everything I do returns some form of 19691231_1800

Code:
$formdatetime = $_POST['hidtimestamp'];
echo $formdatetime;
[b]Returns: 20100128_23:00[/b]


$formtimestamp = date ('Ymd_Hi', strtotime($formdatetime));
echo $formtimestamp;
[b]Returns: 19691231_1800[/b]


$datetime = '20100128 18:02';
$adate = date ('Ymd_Hi', strtotime($datetime));
echo $adate;
[b]Returns: 20100128_1802[/b]


$yourdate = $formdatetime;
$newdate = date('Ymd', mktime(0,0,0, substr($yourdate, 0, 2), substr($yourdate, 2, 2), substr($yourdate, 4, 4)));
print_r($newdate);
[b]Returns: 19691231[/b]
Any ideas why this would be? I've checked the POSTed value, and it is in fact a string, which is why I need to covert to Date object.
There's got to be something special about the date that is being returned 19691231_18:00.
 
as petrosky indicates, this gives you a unix timestamp
Code:
<?php
date_default_timezone_set('Europe/London');
$input = '20100128_18:02';
$unix = strtotime(str_replace('_',' ',$input));
echo date ('r', $unix);
?>

with the unix timestamp you can use the date() function to output in whatever format you want.

however if you trust that the incoming value must always be a valid date of some form then surely you can just do a string replacement operation on the colon?

now to the "date object"? I wonder what you mean: php generally handles dates as unix timestamps (i.e. the number of seconds relative to 1/1/1970 00:00:00 GMT). Which is why you are getting 1969 cropping up, as you are probably one notch or so away from GMT in your current locale.

as php is loosely typed, a string can be handled as an integer or a float or vice versa.

there is a new datetime object introduced into php 5.3. Might that be what you are talking about?

if so then this would give you a new datetime object

Code:
$input = '20100128_18:02';
$dT = new datetime(str_replace('_',' ', $input), new DateTimeZone('Europe/London'));

a datetime object has no format . It is just an object instantiation of the class datetime build in to php. so if you try to do a print_r($dT) you will get no useful information.

to extract information from the datetime object you will need to use one of its methods.

you can find more about the datetime class here:
 
Thank you jpadie for the great information! I didn't realize that the date object for php was new in 5.3, so that helps me understand why I couldn't find much information on it. I was aware of the Unix Epoch, but was NOT aware of the adjustment for GMT position, so that explains the date!
You are correct, I could just do a string replace on the : and use it that way. I had planned to use the actual datetime to compare to another, but decided that was not critical and can just use the string value after replacement.

Your explanation of using datetime in PHP is very good and easily explained the basics that I have been trying to grep for a couple of weeks now. Thank you Thank you for your valuable post!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top