Jump to content

Recommended Posts

I am trying to format a normal date into a timestamp. To convert a timestamp into the normal date was no problem, but now I have an array with 4 different dates! 2 normal dates : 1991-12-09, 12.03.1984 and also 2 timestamps in one array. If I had the same format I could strpos it and make an if and test with "." that worked last time, but now I have those 4 different formats. Anyone an idea?

Link to comment
https://forums.phpfreaks.com/topic/295116-php-date-to-timestamp/
Share on other sites

  • Solution

Are you saying you want to convert all dates in the array to a consistent format? You could do something like this

// dates to convert
$dates = array('1991-12-09', '12.03.1984', '1425560164');

// loop over each date and convert to new format
foreach($dates as $dateToFormat)
{
    // NOTE: unix timestamps need to be prepended with @ symbol
    if(is_numeric($dateToFormat))
        $dateToFormat = '@' . $dateToFormat;
    
    $date = new DateTime($dateToFormat);
    // format date/timestamp to the following format
    echo $date->format('Y-m-d'). '<br />';
}

 

Are you saying you want to convert all dates in the array to a consistent format? You could do something like this

// dates to convert
$dates = array('1991-12-09', '12.03.1984', '1425560164');

// loop over each date and convert to new format
foreach($dates as $dateToFormat)
{
    // NOTE: unix timestamps need to be prepended with @ symbol
    if(is_numeric($dateToFormat))
        $dateToFormat = '@' . $dateToFormat;
    
    $date = new DateTime($dateToFormat);
    // format date/timestamp to the following format
    echo $date->format('Y-m-d'). '<br />';
}

Thanks! Helped me a lot. I worked a little bit around and tested, and found out that you can use -> echo $date->getTimestamp(); 

 

Still thanks!!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.